ManagementEventWatcher 類別

定義

訂閱暫時性事件告知,該告知以指定的「事件查詢」為基礎。

public ref class ManagementEventWatcher : System::ComponentModel::Component
public class ManagementEventWatcher : System.ComponentModel.Component
type ManagementEventWatcher = class
    inherit Component
Public Class ManagementEventWatcher
Inherits Component
繼承
ManagementEventWatcher

範例

下列範例顯示當建立 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
        WqlEventQuery query =
            new WqlEventQuery("__InstanceCreationEvent",
            new TimeSpan(0,0,1),
            "TargetInstance isa \"Win32_Process\"");

        // Initialize an event watcher and subscribe to events
        // that match this query
        ManagementEventWatcher watcher =
            new ManagementEventWatcher();
        watcher.Query = query;
        // times out watcher.WaitForNextEvent in 5 seconds
        watcher.Options.Timeout = new TimeSpan(0,0,5);

        // 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 New WqlEventQuery( _
            "__InstanceCreationEvent", _
            New TimeSpan(0, 0, 1), _
            "TargetInstance isa ""Win32_Process""")

        ' Initialize an event watcher and subscribe to events 
        ' that match this query
        Dim watcher As New ManagementEventWatcher
        watcher.Query = query
        ' times watcher.WaitForNextEvent in 5 seconds
        watcher.Options.Timeout = New TimeSpan(0, 0, 5)

        ' 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

建構函式

ManagementEventWatcher()

初始化 ManagementEventWatcher 類別的新執行個體。 如需進一步初始化,請設定物件上的屬性。 這是無參數建構函式。

ManagementEventWatcher(EventQuery)

當指定 WMI「事件查詢」時,初始化 ManagementEventWatcher 類別的新執行個體。

ManagementEventWatcher(ManagementScope, EventQuery)

初始化 ManagementEventWatcher 類別的新執行個體,此類別會接聽與指定的 WMI「事件查詢」相符的事件。

ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions)

根據指定的選項,初始化 ManagementEventWatcher 類別 (會接聽與指定的 WMI 事件查詢相符的事件) 的新執行個體。 這個變數的查詢和範圍是指定的物件。 選項物件可以指定選項 (例如,逾時和內容資訊)。

ManagementEventWatcher(String)

當以字串形式指定 WMI「事件查詢」時,初始化 ManagementEventWatcher 類別的新執行個體。

ManagementEventWatcher(String, String)

初始化 ManagementEventWatcher 類別的新執行個體,此類別會接聽與指定的 WMI「事件查詢」相符的事件。 這個變數的查詢和範圍會指定為字串。

ManagementEventWatcher(String, String, EventWatcherOptions)

根據指定的選項,初始化 類別的新實例,這個實例 ManagementEventWatcher 會接聽符合指定 WMI 事件查詢的事件。 這個變數的查詢和範圍會指定為字串。 選項物件可以指定選項 (例如,逾時和內容資訊)。

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Container

取得包含 IContainerComponent

(繼承來源 Component)
DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
Options

取得或設定用來監看事件的選項。

Query

取得或設定套用至事件的準則。

Scope

取得或設定要在其中監看事件 (命名空間或範圍) 的範圍。

Site

取得或設定 ComponentISite

(繼承來源 Component)

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Component)
Equals(Object)

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

(繼承來源 Object)
Finalize()

確保已清除未處理的呼叫。 這是物件的解構函式。 在 C# 中,會使用解構函式語法表示完成項。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
Start()

以指定的查詢訂閱事件,並以非同步方式,經由 EventArrived 事件傳遞。

Stop()

取消同步或非同步 (Asynchronous) 訂閱。

ToString()

傳回任何包含 Component 名稱的 String。 不應覆寫此方法。

(繼承來源 Component)
WaitForNextEvent()

等待與指定查詢相符的下一個事件到達,然後將其傳回。

事件

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)
EventArrived

發生於新的事件到達時。

Stopped

發生於訂閱取消時。

適用於