ManagementEventWatcher throws ManagementException with call to Stop()
I have the following piece of code that always throws an exception: The stacktrace is as follows:
System.Management.ManagementException: Shutting down
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.SinkForEventQuery.Cancel()
at System.Management.ManagementEventWatcher.Stop()
at Dell.Client.Framework.Common.RegistryMonitor.StopTreeWatcher()
The code that is causing it is in StopTreeWatcher().
private void StopTreeWatcher()
{
if (bTreeWatcherStarted)
{
if (treeChangeWatcher != null)
treeChangeWatcher.Stop();
bTreeWatcherStarted = false;
}
}
private void StartTreeWatcher()
{
try
{
StopTreeWatcher();
var strQuery = @"SELECT * From RegistryTreeChangeEvent WHERE Hive='HKEY_LOCAL_MACHINE' AND RootPath='" + @regRootPath + "'";
treeChangeWatcher = new ManagementEventWatcher(new WqlEventQuery(strQuery));
treeChangeWatcher.Scope.Path.NamespacePath = @"root\default";
treeChangeWatcher.EventArrived += OnTreeChangeEventArrived;
treeChangeWatcher.Start();
bTreeWatcherStarted = true;
}
catch (Exception)
{
if (throwExceptions)
throw;
}
}
Is this because I am not disposing the ManagementEventWatcher object properly? I don't understand what the "shutting down" message means. But this happens when I initiate a system shutdown. How can I avoid this issue?
The ManagementEventWatcher throws this exception if you call the destructor without Stop() or Dispose(). I suppose that if you have the System.Management.ManagementException with errorCode = ShuttingDown (-2147217357), then you implement a service. So you have to override OnShutdown() in you service, in which you will call dispose for your ManagementEventWatcher. If it is not a service, you have to catch event about system shutdown firstly and then dispose your ManagementEventWatcher. You can also try this code for disposing the treeChangeWatcher. Use lock in multithreaded app.
private void StopTreeWatcher()
{
lock (bTreeWatcherStarted)
{
if (bTreeWatcherStarted)
{
if (treeChangeWatcher != null)
{
treeChangeWatcher.EventArrived -= OnTreeChangeEventArrived;
treeChangeWatcher.Dispose();
treeChangeWatcher = null;
}
bTreeWatcherStarted = false;
}
}
}
ManagementEventWatcher throws ManagementException with call to Stop()的更多相关文章
- 浅谈Java的throw与throws
转载:http://blog.csdn.net/luoweifu/article/details/10721543 我进行了一些加工,不是本人原创但比原博主要更完善~ 浅谈Java异常 以前虽然知道一 ...
- java中的throw与throws的区别
什么时运行时异常?什么是非运行时异常? 通俗的讲: 运行时异常:就是编译通过,运行时就崩了,比如数组越界. 非运行时异常:就是编译不通过,这时就得必须去处理了.不然就没法运行了. 全面的讲: Thro ...
- Java throws Exception、try、catch
throws Exception是方法后面接的 意思是向上级抛出异常 try{}里面的异常会被外面的catch捕捉到 抛出异常是throw new Exception("异常"); ...
- Java throws子句是怎么写的呢?
如果一个方法可以导致一个异常但不处理它,它必须指定这种行为以使方法的调用者可以保护它们自己而不发生异常.做到这点你可以在方法声明中包含一个throws子句.一个 throws 子句列举了一个方法可能抛 ...
- throw与throws的区别
throws语句 throws总是出现在一个函数头中,用来标明该成员函数可能抛出的各种异常.对大多数Exception子类来说,Java 编译器会强迫你声明在一个成员函数中抛出的异常的 ...
- java自定义异常(Exception、throws、try-catch)
一.What is ... 异常处理就是容错处理机制.通过构造一个陷阱来捕获运行时的可预见错误,经对该错误进行适当处理后,让程序能继续运行不至于崩溃. 二.Who will ... 异常由系统环境引发 ...
- try-catch和throw,throws的区别
java里的异常多种多样,这是一种非常有用的机制,它能帮助我们处理那些我们未知的错误,在java里,关于异常的有throw throws,还有一个try catch 程序块.接下来我们挨个看看这几个的 ...
- Java关键字——throws和throw
throws关键字 在定义一个方法时,可以使用throws关键字声明,使用throws声明的方法表示此方法不处理异常,而交给方法的调用处进行处理. 使用了throws关键字,表示不管是否会有异常,在调 ...
- (转载)throw和throws的区别
1.throw:(针对对象的做法)抛出一个异常,可以是系统定义的,也可以是自己定义的.下面举两个例子:抛出Java中的一个系统异常:public class One {public void yich ...
随机推荐
- java基础---数组的排序算法(3)
一.排序的基本概念 排序:将一个数据元素集合或序列重新排列成按一个数据元素某个数据项值有序的序列 稳定排序:排序前和排序后相同元素的位置关系与初始序列位置一致(针对重复元素来说,相对位置不变) 不稳定 ...
- [刘阳Java]_CSS菜单侧边栏制作
再来写一个菜单侧边栏的制作,先看截图 源代码如下 <!DOCTYPE html> <html> <head> <meta charset="UTF- ...
- TCP协议与HTTP协议区别
一.TCP协议与HTTP协议区别 1.直观认识 TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性.Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页 ...
- 添加底部导航栏tabbar
效果图: 如果要添加底部导航栏,最少2个,最多5个. app.json { "pages": [ "pages/index/index", "page ...
- maven之---资源过滤 在java/main/resourse/*.xml ,*.properties引用maven属性${db.username}
本文主要来源maven实战14.3 为了应对环境的变化,首先使用Maven属性将这个会发生变化的部分提取出来.在上一节的数据库配置中,连接数据库使用的驱动类,URL,用户名和密码都可能发生变化,因此使 ...
- Tomcat PUT方法任意写文件漏洞(CVE-2017-12615)
Apache Tomcat 7.0.0~7.0.79 直接发送以下数据包即可在Web根目录写入shell: PUT /1.jsp/ HTTP/1.1 Host: 192.168.49.2:8080 A ...
- PHP-FPM 远程代码执行漏洞(CVE-2019-11043)
影响范围 在 Nginx + PHP-FPM 环境下,当启用了上述 Nginx 配置后,以下 PHP 版本受本次漏洞影响,另外,PHP 5.6版本也受此漏洞影响,但目前只能 Crash,不可以远程代码 ...
- CFS靶机
nmap扫描 nmap -sV -p1-65535 192.168.1.135 thinkphp5.0版本 找到poc进行测试 http://192.168.1.135/index.php?s=ind ...
- Ghost ,博客系统代名词
Ghost 博客系统是前 WordPress 的一些优秀员工的创业项目.项目仍然采用 100% 开源,加上官方的收费托管的商业模式,目前已经发展成为一个非常的博客系统. 为了更好的方便读者理解 Gho ...
- 干了8年Android开发熬到年薪40万,突然接到被辞退消息,应该怎么办?
01 36岁Android开发,为公司工作8年,昨天HR说公司不准备续约 前天晚上,有个读者给我留言,讲述了他自己比较气愤的一件事,感觉自己委屈又不值. 这位朋友不愿意透露姓名,就叫他H先生吧. H先 ...