EventWatcherOptions 类

定义

指定用于管理事件监视的选项。

public ref class EventWatcherOptions : System::Management::ManagementOptions
public class EventWatcherOptions : System.Management.ManagementOptions
type EventWatcherOptions = class
    inherit ManagementOptions
Public Class EventWatcherOptions
Inherits ManagementOptions
继承
EventWatcherOptions

示例

以下示例演示在创建 Win32_Process 实例时客户端如何接收通知,因为事件类 __InstanceCreationEvent。 有关详细信息,请参阅 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)

适用于