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 ...
随机推荐
- 卧槽,原来不需要FQ就可以构建海外镜像
一. 背景 使用docker或者k8s的过程中,我们可能遇到镜像无法下载的情况,例如:kubernetes的kube-apiserver镜像,这是因为其仓库在海外,我们的网络被墙,我发获取到该资源,使 ...
- web系统国际化思路
需求:php开发多个中文系统支持国际化 思路: 提炼各个系统中的中文字符,替换为资源key. 多媒体文件中的中文定位(图片中的中文,中文录音,中文视频,中文模板等). 统一翻译文字.准备资源文件. 各 ...
- 跟我一起学Go系列:gRPC 全局数据传输和超时处理
gRPC 在多个 GoRoutine 之间传递数据使用的是 Go SDK 提供的 Context 包.关于 Context 的使用可以看我之前的一篇文章:Context 使用. 但是 Context ...
- ES6 模块export import
在 ES6 前, 实现模块化使用的是 RequireJS 或者 seaJS(分别是基于 AMD 规范的模块化库, 和基于 CMD 规范的模块化库).ES6 引入了模块化,其设计思想是在编译时就能确定模 ...
- 创建Rdemo项目
1.创建项目工作目录 mkdir /home/sesa464509/R/demo cd /home/sesa464509/R/demo vi sayHello.R ------------------ ...
- 每天五分钟Go - 数组
//数组的声明,默认为0值 var a1 [2]int fmt.Println(a1) //数组的长度 fmt.Println(len(a1)) //遍历数组 for i, v := range a1 ...
- SDN与OpenFlow架构--初识
一,为什么需要SDN 1,传统网络的缺点: a,传统网络及其设备的只可配置,不可编程,只能按照已定义好的协议处理或转发数据,不能适应需求新变化,不能自主开发新功能. 如购买一个电饭煲,可以煮饭,煲汤. ...
- synchronized锁机制(六)
前言 1.理解同步关键词synchronized 2.同步方法与同步代码块的区别 3.理解锁的对象this 脏读 一个常见的概念.在多线程中,难免会出现在多个线程中对同一个对象的实例变量进行并发访问的 ...
- 【系统学习ES6】第一节:新的声明方式
[系统学习ES6] 本专题旨在对ES6的常用技术点进行系统性梳理,帮助大家对其有更好的掌握.计划每周更新1-2篇,希望大家有所收获. 以前用ES5时,声明变量只能用var.ES6的出现,为我们带来了两 ...
- 构建后端第6篇之---java 多态的本质 父类引用 指向子类实现
张艳涛写于2021-2-20 今天来个破例了,不用英文写了,今天在家里电脑写的工具不行,简单的说 主题是:java多态的原理与实现 结论是:java的多态 Father father= new Son ...