ManagementEventWatcher イベント

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。 ( Component から継承されます。) |
![]() | EventArrived | 新しいイベントが到達したときに発生します。 |
![]() | Stopped | サブスクリプションがキャンセルされたときに発生します。 |

ManagementEventWatcher クラス
アセンブリ: System.Management (system.management.dll 内)


イベント クラスが __InstanceCreationEvent であるために、Win32_Process のインスタンスが作成されたときにクライアントが通知を受信するしくみを説明する例を次に示します。詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/ja) で Windows Management Instrumentation に関するドキュメントを参照してください。クライアントは WaitForNextEvent メソッドを呼び出して、イベントを同期的に受信します。この例は、コード例の実行中にメモ帳などのプロセスを開始することでテストできます。
Imports System 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 'EventWatcherPolling
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; } }

System.MarshalByRefObject
System.ComponentModel.Component
System.Management.ManagementEventWatcher


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ManagementEventWatcher コンストラクタ ()
アセンブリ: System.Management (system.management.dll 内)


イベント クラスが __InstanceCreationEvent であるために、Win32_Process のインスタンスが作成されたときにクライアントが通知を受信するしくみを説明する例を次に示します。詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/ja) で Windows Management Instrumentation に関するドキュメントを参照してください。クライアントは WaitForNextEvent メソッドを呼び出して、イベントを同期的に受信します。この例は、コード例の実行中にメモ帳などのプロセスを開始することでテストできます。
Imports System 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 'EventWatcherPolling
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; } }


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ManagementEventWatcher コンストラクタ (ManagementScope, EventQuery)
アセンブリ: System.Management (system.management.dll 内)

Dim scope As ManagementScope Dim query As EventQuery Dim instance As New ManagementEventWatcher(scope, query)
public ManagementEventWatcher ( ManagementScope scope, EventQuery query )
public: ManagementEventWatcher ( ManagementScope^ scope, EventQuery^ query )
public ManagementEventWatcher ( ManagementScope scope, EventQuery query )

このコード例では、イベント クラスが __InstanceCreationEvent であるため、Win32_Process のインスタンスが作成されたときにクライアントが通知を受信します。詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/ja) で Windows Management Instrumentation に関するドキュメントを参照してください。クライアントは WaitForNextEvent メソッドを呼び出して、イベントを同期的に受信します。この例は、コード例の実行中にメモ帳などのプロセスを開始することでテストできます。
Imports System 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""" ' Initialize an event watcher and subscribe to events ' that match this query Dim watcher As New ManagementEventWatcher( _ New ManagementScope("root\CIMV2"), _ New EventQuery(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 'EventWatcherPolling
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\""; // Initialize an event watcher and subscribe to events // that match this query ManagementEventWatcher watcher = new ManagementEventWatcher( new ManagementScope("root\\CIMV2"), new EventQuery(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; } }


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ManagementEventWatcher コンストラクタ (String, String, EventWatcherOptions)
アセンブリ: System.Management (system.management.dll 内)

Dim scope As String Dim query As String Dim options As EventWatcherOptions Dim instance As New ManagementEventWatcher(scope, query, options)
public function ManagementEventWatcher ( scope : String, query : String, options : EventWatcherOptions )

イベント クラスが __InstanceCreationEvent であるために、Win32_Process のインスタンスが作成されたときにクライアントが通知を受信するしくみを説明する例を次に示します。詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/ja) で Windows Management Instrumentation に関するドキュメントを参照してください。クライアントは WaitForNextEvent メソッドを呼び出して、イベントを同期的に受信します。この例は、コード例の実行中にメモ帳などのプロセスを開始することでテストできます。
Imports System 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, so wait for 1 event to return Dim options As New EventWatcherOptions( _ Nothing, TimeSpan.MaxValue, 1) ' Initialize an event watcher and subscribe to events ' that match this query Dim watcher As New ManagementEventWatcher( _ "root\CIMV2", _ query, _ options) ' 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 'EventWatcherPolling
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, so wait for 1 event to return EventWatcherOptions options = new EventWatcherOptions( null, TimeSpan.MaxValue, 1); // Initialize an event watcher and subscribe to events // that match this query ManagementEventWatcher watcher = new ManagementEventWatcher( new ManagementScope("root\\CIMV2"), new EventQuery(query), options); // 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; } }


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ManagementEventWatcher コンストラクタ (ManagementScope, EventQuery, EventWatcherOptions)
アセンブリ: System.Management (system.management.dll 内)

Public Sub New ( _ scope As ManagementScope, _ query As EventQuery, _ options As EventWatcherOptions _ )
Dim scope As ManagementScope Dim query As EventQuery Dim options As EventWatcherOptions Dim instance As New ManagementEventWatcher(scope, query, options)
public ManagementEventWatcher ( ManagementScope scope, EventQuery query, EventWatcherOptions options )
public: ManagementEventWatcher ( ManagementScope^ scope, EventQuery^ query, EventWatcherOptions^ options )
public ManagementEventWatcher ( ManagementScope scope, EventQuery query, EventWatcherOptions options )
public function ManagementEventWatcher ( scope : ManagementScope, query : EventQuery, options : EventWatcherOptions )

イベント クラスが __InstanceCreationEvent であるために、Win32_Process のインスタンスが作成されたときにクライアントが通知を受信するしくみを説明する例を次に示します。詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/ja) で Windows Management Instrumentation に関するドキュメントを参照してください。クライアントは WaitForNextEvent メソッドを呼び出して、イベントを同期的に受信します。この例は、コード例の実行中にメモ帳などのプロセスを開始することでテストできます。
Imports System 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, so wait for 1 event to return Dim options As New EventWatcherOptions( _ Nothing, TimeSpan.MaxValue, 1) ' Initialize an event watcher and subscribe to events ' that match this query Dim watcher As New ManagementEventWatcher( _ New ManagementScope("root\CIMV2"), _ New EventQuery(query), _ options) ' 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 'EventWatcherPolling
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, so wait for 1 event to return EventWatcherOptions options = new EventWatcherOptions( null, TimeSpan.MaxValue, 1); // Initialize an event watcher and subscribe to events // that match this query ManagementEventWatcher watcher = new ManagementEventWatcher( new ManagementScope("root\\CIMV2"), new EventQuery(query), options); // 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; } }


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ManagementEventWatcher コンストラクタ (EventQuery)
アセンブリ: System.Management (system.management.dll 内)

Public Sub New ( _ query As EventQuery _ )
public ManagementEventWatcher ( EventQuery query )
public: ManagementEventWatcher ( EventQuery^ query )
public ManagementEventWatcher ( EventQuery query )
public function ManagementEventWatcher ( query : EventQuery )


このコード例では、イベント クラスが __InstanceCreationEvent であるため、Win32_Process のインスタンスが作成されたときにクライアントが通知を受信します。詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/ja) で Windows Management Instrumentation に関するドキュメントを参照してください。クライアントは WaitForNextEvent メソッドを呼び出して、イベントを同期的に受信します。この例は、コード例の実行中にメモ帳などのプロセスを開始することでテストできます。
Imports System 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""" ' Initialize an event watcher and subscribe to events ' that match this query Dim watcher As New ManagementEventWatcher(New EventQuery( _ 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 'EventWatcherPolling
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\""; // Initialize an event watcher and subscribe to events // that match this query ManagementEventWatcher watcher = new ManagementEventWatcher(new EventQuery(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; } }


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ManagementEventWatcher コンストラクタ (String, String)
アセンブリ: System.Management (system.management.dll 内)


イベント クラスが __InstanceCreationEvent であるために、Win32_Process のインスタンスが作成されたときにクライアントが通知を受信するしくみを説明する例を次に示します。詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/ja) で Windows Management Instrumentation に関するドキュメントを参照してください。クライアントは WaitForNextEvent メソッドを呼び出して、イベントを同期的に受信します。この例は、コード例の実行中にメモ帳などのプロセスを開始することでテストできます。
Imports System 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""" ' Initialize an event watcher and subscribe to events ' that match this query Dim watcher As New ManagementEventWatcher( _ "root\CIMV2", 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 'EventWatcherPolling
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\""; // Initialize an event watcher and subscribe to events // that match this query ManagementEventWatcher watcher = new ManagementEventWatcher("root\\CIMV2", 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; } }


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ManagementEventWatcher コンストラクタ


ManagementEventWatcher コンストラクタ (String)
アセンブリ: System.Management (system.management.dll 内)



イベント クラスが __InstanceCreationEvent であるために、Win32_Process のインスタンスが作成されたときにクライアントが通知を受信するしくみを説明する例を次に示します。詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/ja) で Windows Management Instrumentation に関するドキュメントを参照してください。クライアントは WaitForNextEvent メソッドを呼び出して、イベントを同期的に受信します。この例は、コード例の実行中にメモ帳などのプロセスを開始することでテストできます。
Imports System 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""" ' Initialize an event watcher and subscribe to events ' that match this query Dim watcher As New ManagementEventWatcher(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 'EventWatcherPolling
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\""; // Initialize an event watcher and subscribe to events // that match this query ManagementEventWatcher watcher = new ManagementEventWatcher(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; } }


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ManagementEventWatcher プロパティ

名前 | 説明 | |
---|---|---|
![]() | Container | Component を格納している IContainer を取得します。 ( Component から継承されます。) |
![]() | Options | イベントをウォッチするために使用するオプションを取得または設定します。 |
![]() | Query | イベントに適用する基準を取得または設定します。 |
![]() | Scope | イベントをウォッチするスコープ (名前空間またはスコープ) を取得または設定します。 |
![]() | Site | Component の ISite を取得または設定します。 ( Component から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。 ( Component から継承されます。) |

ManagementEventWatcher メソッド

名前 | 説明 | |
---|---|---|
![]() | CreateObjRef | リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 ( MarshalByRefObject から継承されます。) |
![]() | Dispose | オーバーロードされます。 Component によって使用されているリソースを解放します。 ( Component から継承されます。) |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | InitializeLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | Start | 指定したクエリでイベントをサブスクライブし、EventArrived イベントを通じて、非同期的に配信します。 |
![]() | Stop | 同期的か、非同期的かにかかわらず、サブスクリプションをキャンセルします。 |
![]() | ToString | Component の名前を格納している String を返します (存在する場合)。このメソッドはオーバーライドできません。 ( Component から継承されます。) |
![]() | WaitForNextEvent | 指定したクエリに一致する次のイベントが到達するのを待機してから、そのイベントを返します。 |

名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 Component によって使用されているリソースを解放します。 ( Component から継承されます。) |
![]() | Finalize | オーバーライドされます。 未解決の呼び出しを確実にクリアします。これはオブジェクトのデストラクタです。C# では、ファイナライザはデストラクタ構文を使用して表します。 |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 ( Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |

ManagementEventWatcher メンバ
指定したイベント クエリに基づいて、一時的なイベント通知をサブスクライブします。
ManagementEventWatcher データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Container | Component を格納している IContainer を取得します。(Component から継承されます。) |
![]() | Options | イベントをウォッチするために使用するオプションを取得または設定します。 |
![]() | Query | イベントに適用する基準を取得または設定します。 |
![]() | Scope | イベントをウォッチするスコープ (名前空間またはスコープ) を取得または設定します。 |
![]() | Site | Component の ISite を取得または設定します。(Component から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。(Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。(Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。(Component から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | CreateObjRef | リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 (MarshalByRefObject から継承されます。) |
![]() | Dispose | オーバーロードされます。 Component によって使用されているリソースを解放します。 (Component から継承されます。) |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | InitializeLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | Start | 指定したクエリでイベントをサブスクライブし、EventArrived イベントを通じて、非同期的に配信します。 |
![]() | Stop | 同期的か、非同期的かにかかわらず、サブスクリプションをキャンセルします。 |
![]() | ToString | Component の名前を格納している String を返します (存在する場合)。このメソッドはオーバーライドできません。 (Component から継承されます。) |
![]() | WaitForNextEvent | 指定したクエリに一致する次のイベントが到達するのを待機してから、そのイベントを返します。 |

名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 Component によって使用されているリソースを解放します。 (Component から継承されます。) |
![]() | Finalize | オーバーライドされます。 未解決の呼び出しを確実にクリアします。これはオブジェクトのデストラクタです。C# では、ファイナライザはデストラクタ構文を使用して表します。 |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 (Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。(Component から継承されます。) |
![]() | EventArrived | 新しいイベントが到達したときに発生します。 |
![]() | Stopped | サブスクリプションがキャンセルされたときに発生します。 |

- ManagementEventWatcherのページへのリンク