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 ...
随机推荐
- Mysql学生课程表SQL面试集合
现有如下2个表,根据要求写出SQL语句. student表:编号(sid),姓名(sname),性别(sex) course表:编号(sid),科目(subject),成绩(score) 问题1:查 ...
- P4827「国家集训队」 Crash 的文明世界
「国家集训队」 Crash 的文明世界 提供一种不需要脑子的方法. 其实是看洛谷讨论版看出来的( (但是全网也就这一篇这个方法的题解了) 首先这是一个关于树上路径的问题,我们可以无脑上点分治. 考虑当 ...
- python 常用命令sys.exit()
Python的程序有两中退出方式:os._exit(), sys.exit() os._exit()会直接将python程序终止,之后的所有代码都不会继续执行. sys.exit()会引发一个异常:S ...
- File类与常用IO流第八章——缓冲流
第八章.缓冲流 缓冲流概述 缓冲流,也叫高效流,是对4个基本的FileXxx流的增强.按照数据类型分为4类: 输入缓冲流 输出缓冲流 字节缓冲流 BufferedInputStream Buffe ...
- Spring总结之事务
Spring事务 1)定义 事务是指多个操作单元组成的集合,多个操作单元是整体不可分割的,要么都成功,要么都不成功.必须遵守四个原则(ACID) ●原子性(Atomicity):即事务是不可分割的最小 ...
- MapReduce学习总结之Combiner、Partitioner、Jobhistory
一.Combiner 在MapReduce编程模型中,在Mapper和Reducer之间有一个非常重要的组件,主要用于解决MR性能瓶颈问题 combiner其实属于优化方案,由于带宽限制,应该尽量ma ...
- dubbo(四)
前言 1.浅谈架构的发展 首先,要了解dubbo,就得了解,它是在什么背景下产生的?这就需要从架构的发展说起. 孟老师从事软件开发2008年份,那时候我上高一,那个时候,淘宝.京东都还没有火起来.那个 ...
- 使用Pycharm创建Django项目无法创建app.
Python3.7使用Django1.11.7创建Django项目报以下错误时: 在使用Pycharm创建Django项目报以下错误时: Traceback (most recent call las ...
- 深入刨析tomcat 之---第11篇 how tomcat works( 第15章 ) 如何解析web.xml 文件
writedby 张艳涛 记得当年是学习jsp的时候,写过web.xml中的标签.在之后的springmvc中也是有关于配置mvc 过滤器 和dispatchServlet的标签,之前是看不懂呢!看到 ...
- 深入刨析tomcat 之---第2篇,解决第3章bug 页面不显示内容http://localhost:8080/servlet/ModernServlet?userName=zhangyantao&password=1234
writedby 张艳涛7月2日, 在学习第4张的过程中,发现了前一篇文章写的是关于1,2张的bug不用设置response响应头,需要在servlet的service()方法里面写是错误想 serv ...