EventWatcherOptions 构造函数

定义

初始化监视事件的 EventWatcherOptions 类的新实例。

重载

EventWatcherOptions()

使用默认值为事件监视初始化 EventWatcherOptions 类的新实例。 这是无参数构造函数。

EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32)

使用给定的值初始化 EventWatcherOptions 类的新实例。

EventWatcherOptions()

Source:
ManagementOptions.cs
Source:
ManagementOptions.cs
Source:
ManagementOptions.cs

使用默认值为事件监视初始化 EventWatcherOptions 类的新实例。 这是无参数构造函数。

public:
 EventWatcherOptions();
public EventWatcherOptions ();
Public Sub New ()

示例

以下示例演示由于事件类__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

注解

.NET Framework 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库

适用于

EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32)

Source:
ManagementOptions.cs
Source:
ManagementOptions.cs
Source:
ManagementOptions.cs

使用给定的值初始化 EventWatcherOptions 类的新实例。

public:
 EventWatcherOptions(System::Management::ManagementNamedValueCollection ^ context, TimeSpan timeout, int blockSize);
public EventWatcherOptions (System.Management.ManagementNamedValueCollection context, TimeSpan timeout, int blockSize);
new System.Management.EventWatcherOptions : System.Management.ManagementNamedValueCollection * TimeSpan * int -> System.Management.EventWatcherOptions
Public Sub New (context As ManagementNamedValueCollection, timeout As TimeSpan, blockSize As Integer)

参数

context
ManagementNamedValueCollection

包含要传递给提供程序的、提供程序特定的信息的选项上下文对象。

timeout
TimeSpan

等待下一个事件的超时时间。

blockSize
Int32

每个块中要等待的事件数。

示例

以下示例演示由于事件类__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
        // blockSize = 1 to receive one event
        EventWatcherOptions eventOptions = new
            EventWatcherOptions(null, System.TimeSpan.MaxValue,
            1);

        // 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
        ' blockSize = 1 to receive one event
        Dim eventOptions As New EventWatcherOptions( _
            Nothing, System.TimeSpan.MaxValue, 1)

        ' 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

注解

.NET Framework 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库

适用于