語言

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

範例

以下範例說明當事件類別被__InstanceCreationEvent時,客戶端如何收到Win32_Process實例的通知。 欲了解更多資訊,請參閱 Windows 管理儀器文件。 用戶端透過呼叫 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

建構函式

名稱 Description
EventWatcherOptions()

初始化該類別的新實例 EventWatcherOptions 用於事件監視,使用預設值。 這就是無參數建構子。

EventWatcherOptions(ManagementNamedValueCollection, TimeSpan, Int32)

初始化一個新的類別實例 EventWatcherOptions ,並以給定的值。

屬性

名稱 Description
BlockSize

取得或設定區塊操作的區塊大小。 等待事件時,此值指定等待多少事件後才返回。

Context

取得或設定一個 WMI 上下文物件。 這是一個名稱與值對的清單,需傳遞給支援上下文資訊以供自訂操作的 WMI 提供者。

(繼承來源 ManagementOptions)
Timeout

取得或設定該操作適用的逾時。 請注意,對於回傳集合的操作,此逾時適用於透過所得集合的列舉,而非操作本身(後者使用該 ReturnImmediately 屬性)。 此特性用來表示操作應半同步執行。

(繼承來源 ManagementOptions)

方法

名稱 Description
Clone()

回傳物件的副本。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於