需要帮助WMI ManagementEventWatcher无效的类异常` [英] Need Help WMI ManagementEventWatcher Invalid Class Exception`

查看:81
本文介绍了需要帮助WMI ManagementEventWatcher无效的类异常`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在与我刚开始时以为是一个小问题的斗争,而现在却使我陷入困惑的一天.我试图实例化一个ManagementEventWatcher来监视我的PC上的时钟并仅显示时间.问题 我遇到的问题是,观察者一启动,它就会抛出一个无效类,该类将作为管理异常抛出.

I've been battling what I thought would be a small problem when I started it, to what has now caused me to a day or so of puzzlement. I am trying to instantiate a ManagementEventWatcher to monitor the clock on my pc and simply display the time. The problem I am running into is that as soon as the watcher starts, it throws and invalid class that is being thrown as a management exception.

我在论坛上关注了一些话题,并认为可能需要为观察者指定一个事件类名称,但是我认为我将在此处发布代码,以便也许有人会看到我正在创建的问题.为我自己.

I followed a few threads here on the forums, and thought it might be that I need to specify an eventclassname for the watcher, but I thought I would post the code here, so that perhaps someone might see the problem I am creating for myself.

由于我与WMI的合作不多,因此非常感谢任何帮助!

As I have not worked much with WMI, any help is much appreciated!

平台:Windows 7

Platform: Windows 7

Visual Studio 2010

Visual Studio 2010

----------代码开始------------------------

---------- code begins------------------------

    ///< summary>
    ///MainWindow.xaml的交互逻辑
    ///</summary>
   公共局部类MainWindow:Window
    {
       ManagementEventWatcher watcher =新的ManagementEventWatcher();

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ManagementEventWatcher watcher = new ManagementEventWatcher();

公共MainWindow()
       {
           InitializeComponent();
       }

        public MainWindow()
        {
            InitializeComponent();
        }

私有void Window_Loaded(对象发送者,RoutedEventArgs e)
       {
           MessageBox.Show(该应用已启动.");
           //此命名空间中定义的eventreceiver
           EventReceiver接收器=新的EventReceiver();

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("The App started..");
            //eventreceiver defined in this namespace
            EventReceiver receiver = new EventReceiver();

//创建观察者并注册回调
           watcher = GetWatcher(new EventArrivedEventHandler(receiver.OnEventArrived));
           //观察者开始收听管理事件
          试试
           {
                             watcher.Start();
           }
          抓住(AccessViolationException ex)
           {
                             MessageBox.Show(ex.Message +访问异常");
           }
          捕捉(ManagementException ex)
           {
                             MessageBox.Show(ex.Message +"management exception");
           }
           catch(异常例外)
           {
                             MessageBox.Show(ex.Message +一般异常");
           }  
       }

            //create watcher and register the callback
            watcher = GetWatcher(new EventArrivedEventHandler(receiver.OnEventArrived));
            //watcher starts to listen to management events
            try
            {
                watcher.Start();
            }
            catch (AccessViolationException ex)
            {
                MessageBox.Show(ex.Message + " access exception");
            }
            catch (ManagementException ex)
            {
                MessageBox.Show(ex.Message + " management exception");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " general exception");
            }  
        }

私有void Window_Closing(对象发送者,System.ComponentModel.CancelEventArgs e)
       {
           watcher.Stop();
       }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            watcher.Stop();
        }

公共静态ManagementEventWatcher GetWatcher(EventArrivedEventHandler处理程序)
       {
           //创建事件查询,以便在服务更改后1秒钟内得到通知
           WqlEventQuery查询=新的WqlEventQuery("_ InstanceModificationEvent",新的TimeSpan(0,0,1),
                             "TargetInstance是一个'Win32_Localtime'AND". +" TargetInstance.Second = 0");
           ManagementScope范围=新的ManagementScope(@" \\.\ root \ default "));
           //初始化事件观察者并订阅与该查询匹配的事件
           ManagementEventWatcher watcher =新的ManagementEventWatcher(scope,query);
           //使用
           //必需的处理程序,以允许观察者与应用进行通信
           watcher.EventArrived + =新的EventArrivedEventHandler(handler);
          返回观察者;
       }
    }

        public static ManagementEventWatcher GetWatcher(EventArrivedEventHandler handler)
        {
            //create event query to be notified within 1 second of a change in a service
            WqlEventQuery query = new WqlEventQuery("_InstanceModificationEvent", new TimeSpan(0, 0, 1),
                "TargetInstance isa 'Win32_Localtime' AND " + "TargetInstance.Second = 0");
            ManagementScope scope = new ManagementScope(@"\\.\root\default");
            //initialize an event watcher and subscribe to events that match this query
            ManagementEventWatcher watcher = new ManagementEventWatcher(scope,query);
            //attach the event arrived property to eventarrivedeventhandler method with the
            //required handler to allow watcher to communicate to the app
            watcher.EventArrived += new EventArrivedEventHandler(handler);
            return watcher;
        }
    }

   公共类EventReceiver
    {
      公共无效OnEventArrived(对象发送者,EventArrivedEventArgs e)
       {
           ManagementBaseObject evt = e.NewEvent;

    public class EventReceiver
    {
        public void OnEventArrived(object sender, EventArrivedEventArgs e)
        {
            ManagementBaseObject evt = e.NewEvent;

//显示事件的信息
          字符串时间= string.Format("{0}:{1:00}",
                             ((ManagementBaseObject)evt ["TargetInstance"]] ["Hour"],
                             ((ManagementBaseObject)evt ["TargetInstance"]] ["Minute"]));

            //display information for the event
            string time = string.Format("{0}:{1:00}",
                ((ManagementBaseObject)evt["TargetInstance"])["Hour"],
                ((ManagementBaseObject)evt["TargetInstance"])["Minute"]);

MessageBox.Show(时间,当前时间");
       }
    }

            MessageBox.Show(time, "Current time");
        }
    }

-------代码结尾-------

 

 

推荐答案

尝试使用:

          //WqlEventQuery query = new WqlEventQuery();
//            query.EventClassName =" __ InstanceModificationEvent" ;;
//            query.Condition = @"quot; TargetInstance ISA'Win32_LocalTime'AND TargetInstance.Hour = 0 AND
//TargetInstance.Minute = 0 AND TargetInstance.Second = 0" ;;

           // WqlEventQuery query = new WqlEventQuery();
//            query.EventClassName = "__InstanceModificationEvent ";
//            query.Condition = @"TargetInstance ISA 'Win32_LocalTime' AND TargetInstance.Hour = 0 AND
//TargetInstance.Minute = 0 AND TargetInstance.Second = 0";

如何使用计时器对象?

哈里

 


这篇关于需要帮助WMI ManagementEventWatcher无效的类异常`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆