首页 > 其他分享 >ManagementEventWatcher throws ManagementException with call to Stop()

ManagementEventWatcher throws ManagementException with call to Stop()

时间:2023-06-09 10:04:25浏览次数:46  
标签:Management StopTreeWatcher Stop System bTreeWatcherStarted call ManagementEventW

参考网址: https://stackoverflow.com/questions/46100105/managementeventwatcher-throws-managementexception-with-call-to-stop

0

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?

c#.netmanagementeventwatcher

Share

Improve this question

Follow

 

asked Sep 7 '17 at 15:26

Sai

66222 gold badges1111 silver badges3131 bronze badges

Add a comment


1 Answer

ActiveOldestVotes


2

 

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;
        }
    }
}

Share

Improve this answer

Follow

 

 



标签:Management,StopTreeWatcher,Stop,System,bTreeWatcherStarted,call,ManagementEventW
From: https://blog.51cto.com/u_4018548/6445479

相关文章

  • JAVA 线程池之Callable返回结果
    JAVA线程池之Callable返回结果原文:https://www.cnblogs.com/hapjin/p/7599189.html本文介绍如何向线程池提交任务,并获得任务的执行结果。然后模拟线程池中的线程在执行任务的过程中抛出异常时,该如何处理。一、执行具体任务的线程类要想获得线程的执行结果,需实现Callable接......
  • VUE Error: Cannot call .tap() on a plugin that has not yet been defined. Call pl
    在对一个vue项目执行过“npmauditfix--force”命令之后,就出现了如下错误: ERROR Error:Cannotcall.tap()onapluginthathasnotyetbeendefined.Callplugin('preload').use(<Plugin>)first.有2个解决方法:方法一:删除之前的源码模块,重新下载后执行“npminstall......
  • GYM100212B - I Just Called...
    大模拟。首先的难度在于理解题意:打电话的地点分为镇、地区、超级地区三级。其中,一些地区是被网络连接的。电话号码的前缀由地区号+镇号组成。它们可以是不等长的,但是整个电话号码的长度是\(d\)。一个镇可能有多个镇号,不同地区的镇可以拥有相同的镇号,但地区号是唯一的。同时......
  • DRTREE - Dynamically-Rooted Tree 题解
    DRTREE-Dynamically-RootedTree本题建议评蓝。思路:题目就是要对一颗不定根树求子树权值和。这题不带修,如果带修难度会增加一点,就跟遥远的国度差不多。首先分析一下在以不同根下子树的变化。当一颗树以1号节点为根时,比如说长这样:假设每个点的权值为1,那么这8个点......
  • 解决 NVIDIA Windows has stopped this device because it has reported problems. (C
    场景当跑需要使用GPU算力的一些项目时候,需要用到CUDA,确保电脑是具有独立显卡的机子,但是怎么也没法让代码中的torch跑在GPU上;点击任务管理器查看"性能"下的GPU选项,看到运行中的并非是独立显卡而是集成显卡;点击设备管理器,发现NVIDIA显卡左下角有感叹号,双击发现里面显示"Wind......
  • CALL n10s.rdf.import.fetch('~/env/datas/marvel.nt', 'N-Triples')路径应该如何定义
    在Neo4j中使用n10s.rdf.import.fetch()函数导入RDF数据时,路径的定义方式取决于你运行Neo4j数据库的操作系统和文件系统的配置。在给定路径之前,请确保你具有适当的文件系统权限。以下是路径定义的示例:在Windows上:CALLn10s.rdf.import.fetch('file:///C:/Users/YourUsername/e......
  • 解决 NVIDIA Windows has stopped this device because it has reported problems. (C
    场景当跑需要使用GPU算力的一些项目时候,需要用到CUDA,确保电脑是具有独立显卡的机子,但是怎么也没法让代码中的torch跑在GPU上;点击任务管理器查看"性能"下的GPU选项,看到运行中的并非是独立显卡而是集成显卡;点击设备管理器,发现NVIDIA显卡左下角有感叹号,双击发现里面显示......
  • 特性—[CallerMemberName]
    [CallerMemberName]是C#中的一个特性(Attribute),用于在方法参数中标记一个字符串参数,用来自动获取调用该方法的成员名称(方法、属性、字段等)。当在方法参数中使用了[CallerMemberName]特性时,如果该参数没有被显式地传入值,编译器会自动将调用方法的成员名称作为参数的默认值。这......
  • Call stack 调用栈理解
    我们在上课讲到递归函数调用的空间复杂度的时候曾多次提到过callstack的概念,然而很多同学表示不太清楚。今天我们就来讲一下callstack是什么。相信有了上一篇文章对virtualmemory的介绍之后,同学们理解起Callstack来会相对容易一些。CallStack是什么?Callstack(通常译作'调用栈......
  • spark context stop use with as
    调用方法:withsession.SparkStreamingSession('CC_Traffic_Realtime',ssc_time_windown)asss_session:kafkaStreams=ss_session.get_direct_stream(TOPICNAME)kafkaStreams.transform(xxxx)...ss_session.ready_to_go()实现方......