EventWatcherOptions 类

定义

指定用于管理事件监视的选项。Specifies options for management event watching.

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的实例时客户端如何收到通知, 因为事件类为 __InstanceCreationEventThe following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. 有关详细信息, 请参阅Windows Management Instrumentation文档。For more information, see the Windows Management Instrumentation documentation. 客户端通过调用 WaitForNextEvent 方法来同步接收事件。The client receives events synchronously by calling the WaitForNextEvent method. 在运行此代码示例的同时,可以启动进程(如记事本)来测试此示例。This example can be tested by starting a process, such as Notepad, while the example code is running.

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 类的新实例。Initializes a new instance of the EventWatcherOptions class for event watching, using default values. 这是无参数构造函数。This is the parameterless constructor.

EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32)

使用给定的值初始化 EventWatcherOptions 类的新实例。Initializes a new instance of the EventWatcherOptions class with the given values.

属性

BlockSize

获取或设置块操作的块大小。Gets or sets the block size for block operations. 等待事件时,此值指定返回前要等待多少事件。When waiting for events, this value specifies how many events to wait for before returning.

Context

获取或设置一个 WMI 上下文对象。Gets or sets a WMI context object. 这是将传递给 WMI 提供程序的名称-值对列表,该提供程序支持自定义操作的上下文信息。This is a name-value pairs list to be passed through to a WMI provider that supports context information for customized operation.

(继承自 ManagementOptions)
Timeout

获取或设置要应用于该操作的超时。Gets or sets the time-out to apply to the operation. 注意,对于返回集合的操作,此超时将通过结果集合(而不是操作本身)应用于枚举(对于后面一种情况,应使用 ReturnImmediately 属性)。Note that for operations that return collections, this time-out applies to the enumeration through the resulting collection, not the operation itself (the ReturnImmediately property is used for the latter). 此属性用来指示将以半同步方式执行操作。This property is used to indicate that the operation should be performed semi-synchronously.

(继承自 ManagementOptions)

方法

Clone()

返回对象的一个副本。Returns a copy of the object.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于