zoukankan      html  css  js  c++  java
  • 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 = @"rootdefault";
            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?

    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;
            }
        }
    }
    
     
  • 相关阅读:
    汽车文化【1196】
    西方经济学
    计算机组成原理【0013】
    C语言程序设计【0039】
    教育学【0405】
    管理学[9080]
    专业英语【0089】
    计算机基础1056
    letcode每日一题-上升下降字符串
    Flowable学习-flowable
  • 原文地址:https://www.cnblogs.com/bruce1992/p/15189465.html
Copyright © 2011-2022 走看看