ManagementEventWatcher.WaitForNextEvent 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
等待出现下一个与指定的查询匹配的事件,然后将该事件返回。
public:
System::Management::ManagementBaseObject ^ WaitForNextEvent();
public System.Management.ManagementBaseObject WaitForNextEvent ();
member this.WaitForNextEvent : unit -> System.Management.ManagementBaseObject
Public Function WaitForNextEvent () As ManagementBaseObject
返回
一个 ManagementBaseObject,它表示刚到达的事件。
示例
以下示例演示由于事件类__InstanceCreationEvent而创建 Win32_Process 实例时客户端如何接收通知。 有关详细信息,请参阅 Windows Management Instrumentation 文档。 客户端通过调用 WaitForNextEvent 方法来同步接收事件。 在运行此代码示例的同时,可以启动进程(如记事本)来测试此示例。
using System;
using System.Management;
// This example shows synchronous consumption of events.
// The client is blocked while waiting for events.
public class EventWatcherPolling
{
public static int Main(string[] args)
{
// Create event query to be notified within 1 second of
// a change in a service
WqlEventQuery query =
new WqlEventQuery("__InstanceCreationEvent",
new TimeSpan(0,0,1),
"TargetInstance isa \"Win32_Process\"");
// Initialize an event watcher and subscribe to events
// that match this query
ManagementEventWatcher watcher =
new ManagementEventWatcher();
watcher.Query = query;
// times out watcher.WaitForNextEvent in 5 seconds
watcher.Options.Timeout = new TimeSpan(0,0,5);
// Block until the next event occurs
// Note: this can be done in a loop if waiting for
// more than one occurrence
Console.WriteLine(
"Open an application (notepad.exe) to trigger an event.");
ManagementBaseObject e = watcher.WaitForNextEvent();
//Display information from the event
Console.WriteLine(
"Process {0} has been created, path is: {1}",
((ManagementBaseObject)e
["TargetInstance"])["Name"],
((ManagementBaseObject)e
["TargetInstance"])["ExecutablePath"]);
//Cancel the subscription
watcher.Stop();
return 0;
}
}
Imports System.Management
' This example shows synchronous consumption of events.
' The client is blocked while waiting for events.
Public Class EventWatcherPolling
Public Overloads Shared Function _
Main(ByVal args() As String) As Integer
' Create event query to be notified within 1 second of
' a change in a service
Dim query As New WqlEventQuery( _
"__InstanceCreationEvent", _
New TimeSpan(0, 0, 1), _
"TargetInstance isa ""Win32_Process""")
' Initialize an event watcher and subscribe to events
' that match this query
Dim watcher As New ManagementEventWatcher
watcher.Query = query
' times watcher.WaitForNextEvent in 5 seconds
watcher.Options.Timeout = New TimeSpan(0, 0, 5)
' Block until the next event occurs
' Note: this can be done in a loop
' if waiting for more than one occurrence
Console.WriteLine( _
"Open an application (notepad.exe) to trigger an event.")
Dim e As ManagementBaseObject = _
watcher.WaitForNextEvent()
'Display information from the event
Console.WriteLine( _
"Process {0} has created, path is: {1}", _
CType(e("TargetInstance"), _
ManagementBaseObject)("Name"), _
CType(e("TargetInstance"), _
ManagementBaseObject)("ExecutablePath"))
'Cancel the subscription
watcher.Stop()
Return 0
End Function 'Main
End Class
注解
如果事件观察程序对象包含具有指定超时的选项,则 API 将仅等待指定时间量的下一个事件;否则,将阻止 API,直到发生下一个事件。
.NET Framework 安全性
对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库。