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

次の例は、イベント クラスが__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
        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 (Windows Management Instrumentation) イベント クエリが指定されたときに、ManagementEventWatcher クラスの新しいインスタンスを初期化します。

ManagementEventWatcher(ManagementScope, EventQuery)

指定した WMI イベント クエリに一致するイベントを待機する ManagementEventWatcher クラスの新しいインスタンスを初期化します。

ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions)

指定したオプションに従って、指定した WMI イベント クエリに一致するイベントを待機する ManagementEventWatcher クラスの新しいインスタンスを初期化します。 このバリアントでは、クエリとスコープは指定したオブジェクトです。 オプション オブジェクトは、タイムアウトやコンテキスト情報などのオプションを指定できます。

ManagementEventWatcher(String)

文字列形式で WMI イベント クエリが指定された場合、ManagementEventWatcher クラスの新しいインスタンスを初期化します。

ManagementEventWatcher(String, String)

指定した WMI イベント クエリに一致するイベントを待機する ManagementEventWatcher クラスの新しいインスタンスを初期化します。 このバリアントでは、クエリとスコープは文字列として指定します。

ManagementEventWatcher(String, String, EventWatcherOptions)

指定したオプションに ManagementEventWatcher 従って、指定された WMI イベント クエリに準拠するイベントをリッスンする クラスの新しいインスタンスを初期化します。 このバリアントでは、クエリとスコープは文字列として指定します。 オプション オブジェクトは、タイムアウトやコンテキスト情報などのオプションを指定できます。

プロパティ

CanRaiseEvents

コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。

(継承元 Component)
Container

IContainer を含む Component を取得します。

(継承元 Component)
DesignMode

Component が現在デザイン モードかどうかを示す値を取得します。

(継承元 Component)
Events

Component に結び付けられているイベント ハンドラーのリストを取得します。

(継承元 Component)
Options

イベントをウォッチするために使用するオプションを取得または設定します。

Query

イベントに適用する基準を取得または設定します。

Scope

イベントをウォッチするスコープ (名前空間またはスコープ) を取得または設定します。

Site

ComponentISite を取得または設定します。

(継承元 Component)

メソッド

CreateObjRef(Type)

リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。

(継承元 MarshalByRefObject)
Dispose()

Component によって使用されているすべてのリソースを解放します。

(継承元 Component)
Dispose(Boolean)

Component によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

(継承元 Component)
Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Finalize()

未解決の呼び出しを確実にクリアします。 これはオブジェクトのデストラクターです。 C# では、ファイナライザーはデストラクター構文を使用して表します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetLifetimeService()
古い.

対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
GetService(Type)

Component またはその Container で提供されるサービスを表すオブジェクトを返します。

(継承元 Component)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
InitializeLifetimeService()
古い.

このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
MemberwiseClone(Boolean)

現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。

(継承元 MarshalByRefObject)
Start()

指定したクエリでイベントをサブスクライブし、EventArrived イベントを通じて、非同期的に配信します。

Stop()

同期的か、非同期的かにかかわらず、サブスクリプションをキャンセルします。

ToString()

Component の名前 (存在する場合) を格納する String を返します。 このメソッドはオーバーライドできません。

(継承元 Component)
WaitForNextEvent()

指定したクエリに一致する次のイベントが到達するのを待機してから、そのイベントを返します。

イベント

Disposed

Dispose() メソッドの呼び出しによってコンポーネントが破棄されるときに発生します。

(継承元 Component)
EventArrived

新しいイベントが到達したときに発生します。

Stopped

サブスクリプションがキャンセルされたときに発生します。

適用対象