C#中的WMI半同步事件(帮助!) [英] wmi semisynchronous events in c# (help!)

查看:75
本文介绍了C#中的WMI半同步事件(帮助!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我对同步和半同步事件感到非常困惑.在阅读开发wmi解决方案"的一本电子书中,它说下面的代码块是半同步的,并且由于事件的性质,同步事件通知"是不可能的.在事件查询注册后的任何时候都可以发送事件".

但是,在MSDN中,使用与下面的代码完全相同的方法的代码被标记为同步代码.
(MSDN, ExecNotificationQuery.

c#中
ExecNotificationQuery的等效项是什么?和下面的代码是真的同步还是半同步?如果实际上是同步的,我该如何使其半同步?


WqlEventQuery query2 = new WqlEventQuery(
""TargetInstance ISA'Win32_Process'" +" AND TargetInstance.Name ='notepad.exe''');

Hi. I'm very confused about synchronous and semi synchronous events. In one ebook im reading "developing wmi solutions", it said the block of code below is semi-synchronous and that "Synchronous Event Notification is not possible because of the nature of events. Events get delivered at any time
after the event query is registered".

However, in MSDN, a code which uses the exact same methods as the code below was labelled as synchronous.
(MSDN, http://msdn.microsoft.com/en-us/library/ms257355(VS.80).aspx). And in MSDN, there is no code example or tutorial on how to write semisynchronous events in c# or .NET, only in VBscript. And it uses ExecNotificationQuery.  

What is the equivalent for
ExecNotificationQuery in c#? and Is the code below really synchronous or semisynchronous? If it is in fact synchronous, how do I make it semisynchronous?


WqlEventQuery query2 = new WqlEventQuery(
             "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE " +
             "TargetInstance ISA 'Win32_Process'" +
             " AND TargetInstance.Name = 'notepad.exe'");

EventWatcherOptions options2 = new EventWatcherOptions();
options2.Timeout = new TimeSpan(0,1,0);

            EventWatcherOptions options2 = new EventWatcherOptions();
            options2.Timeout = new TimeSpan(0, 1, 0);

ManagementEventWatcher watcher2 =新的ManagementEventWatcher(scope,query2,options2);

            ManagementEventWatcher watcher2 = new ManagementEventWatcher(scope, query2, options2);

ManagementBaseObject newEvent2 = watcher2.WaitForNextEvent();
ManagementBaseObject targetInstance2 =(ManagementBaseObject)newEvent2 [" TargetInstance"];

Console.WriteLine(QUOT;收到{0}的事件类型{1},newEvent2 [" __ CLASS],targetInstance2 [" __ CLASS]);

watcher2.Stop();


帮帮忙.任何见识将不胜感激.

,谢谢,
Shiela

                    ManagementBaseObject newEvent2 = watcher2.WaitForNextEvent();
                    ManagementBaseObject targetInstance2 = (ManagementBaseObject)newEvent2["TargetInstance"];
                  
                    Console.WriteLine("Received {0} event of type {1}", newEvent2["__CLASS"], targetInstance2["__CLASS"]);
                    watcher2.Stop();


I need all your help guys. Any insights would be appreciated.

Thanks,
Shiela

推荐答案

查看并尝试可以使用内置工具WBEMTest(按Win键+ R快捷方式并输入此命令,然后按Enter)来区别不同模式之间的差异.

To see and try the difference between different modes you can use built-in tool WBEMTest (press win key + R shortcut and enter this command, then Enter).

在您的示例中,正在执行代码的线程将在以下行被阻止:
  ManagementBaseObject newEvent2 = watcher2.WaitForNextEvent();

In your example, thread that is executing your code will be blocked on this line:
  ManagementBaseObject newEvent2 = watcher2.WaitForNextEvent();

除非任何事件到达或超时到期,所以从.NET角度来看,它以同步方式执行.事实是,半同步执行与WMI本身有关,并且适用于用C ++或VB编写的旧版应用程序,而对于.NET 您只有2种模式:同步和异步.

unless any event arrives or timeout is expired, so that from .NET perspective it is executed in synchronous way. The thing is that semi-synchronous execution is related to WMI itself and intended for legacy application written in C++ or VB, whereas for .NET you have only 2 modes: synchronous and asynchronous.

如果您需要异步模式,请使用以下代码(仅适用于本地计算机):

If you need asynchronous mode, use this code (which works only for local machine):

  eventWatcher.EventArrived + = OnEventArrived;

  eventWatcher.EventArrived + = OnEventArrived;  eventWatcher.Start();

如果还需要连接到远程计算机,请使用以下代码:

If you need to connect to remote machines as well, use this code:

var thread = new Thread(ListenForEvents);
线程  .Start();

var thread = new Thread(ListenForEvents);
thread .Start();

...


这篇关于C#中的WMI半同步事件(帮助!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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