EventWatcherOptions 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定用于管理事件监视的选项。
public ref class EventWatcherOptions : System::Management::ManagementOptions
public class EventWatcherOptions : System.Management.ManagementOptions
type EventWatcherOptions = class
inherit ManagementOptions
Public Class EventWatcherOptions
Inherits ManagementOptions
- 继承
示例
下面的示例演示由于事件类__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
string query = "SELECT * FROM" +
" __InstanceCreationEvent WITHIN 1 " +
"WHERE TargetInstance isa \"Win32_Process\"";
// Event options
EventWatcherOptions eventOptions = new
EventWatcherOptions();
eventOptions.Timeout = System.TimeSpan.MaxValue;
// Initialize an event watcher and subscribe to events
// that match this query
ManagementEventWatcher watcher =
new ManagementEventWatcher("root\\CIMV2", query,
eventOptions);
// 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 String
query = "SELECT * FROM" & _
" __InstanceCreationEvent WITHIN 1 " & _
"WHERE TargetInstance isa ""Win32_Process"""
' Event options
Dim eventOptions As New EventWatcherOptions
eventOptions.Timeout = System.TimeSpan.MaxValue
' Initialize an event watcher and subscribe to events
' that match this query
Dim watcher As New ManagementEventWatcher( _
"root\CIMV2", query, eventOptions)
' 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
构造函数
EventWatcherOptions() |
使用默认值为事件监视初始化 EventWatcherOptions 类的新实例。 这是无参数构造函数。 |
EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32) |
使用给定的值初始化 EventWatcherOptions 类的新实例。 |
属性
BlockSize |
获取或设置块操作的块大小。 等待事件时,此值指定返回前要等待多少事件。 |
Context |
获取或设置一个 WMI 上下文对象。 这是将传递给 WMI 提供程序的名称-值对列表,该提供程序支持自定义操作的上下文信息。 (继承自 ManagementOptions) |
Timeout |
获取或设置要应用于该操作的超时。 注意,对于返回集合的操作,此超时将通过结果集合(而不是操作本身)应用于枚举(对于后面一种情况,应使用 ReturnImmediately 属性)。 此属性用来指示将以半同步方式执行操作。 (继承自 ManagementOptions) |
方法
Clone() |
返回对象的一个副本。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |