ManagementEventWatcher 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 ManagementEventWatcher 類別的新執行個體。
多載
ManagementEventWatcher() |
初始化 ManagementEventWatcher 類別的新執行個體。 如需進一步初始化,請設定物件上的屬性。 這是無參數建構函式。 |
ManagementEventWatcher(EventQuery) |
當指定 WMI「事件查詢」時,初始化 ManagementEventWatcher 類別的新執行個體。 |
ManagementEventWatcher(String) |
當以字串形式指定 WMI「事件查詢」時,初始化 ManagementEventWatcher 類別的新執行個體。 |
ManagementEventWatcher(ManagementScope, EventQuery) |
初始化 ManagementEventWatcher 類別的新執行個體,此類別會接聽與指定的 WMI「事件查詢」相符的事件。 |
ManagementEventWatcher(String, String) |
初始化 ManagementEventWatcher 類別的新執行個體,此類別會接聽與指定的 WMI「事件查詢」相符的事件。 這個變數的查詢和範圍會指定為字串。 |
ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions) |
根據指定的選項,初始化 ManagementEventWatcher 類別 (會接聽與指定的 WMI 事件查詢相符的事件) 的新執行個體。 這個變數的查詢和範圍是指定的物件。 選項物件可以指定選項 (例如,逾時和內容資訊)。 |
ManagementEventWatcher(String, String, EventWatcherOptions) |
根據指定的選項,初始化 類別的新實例 ManagementEventWatcher ,以接聽符合指定WMI 事件查詢的事件。 這個變數的查詢和範圍會指定為字串。 選項物件可以指定選項 (例如,逾時和內容資訊)。 |
ManagementEventWatcher()
初始化 ManagementEventWatcher 類別的新執行個體。 如需進一步初始化,請設定物件上的屬性。 這是無參數建構函式。
public:
ManagementEventWatcher();
public ManagementEventWatcher ();
Public Sub New ()
範例
下列範例顯示當建立 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
備註
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。
適用於
ManagementEventWatcher(EventQuery)
當指定 WMI「事件查詢」時,初始化 ManagementEventWatcher 類別的新執行個體。
public:
ManagementEventWatcher(System::Management::EventQuery ^ query);
public ManagementEventWatcher (System.Management.EventQuery query);
new System.Management.ManagementEventWatcher : System.Management.EventQuery -> System.Management.ManagementEventWatcher
Public Sub New (query As EventQuery)
參數
- query
- EventQuery
EventQuery,表示會判斷監看員將接聽之事件的 WMI 事件查詢。
範例
在此程式代碼範例中,用戶端會在建立 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
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;
}
}
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
備註
監看員要接聽事件的命名空間是目前設定的預設命名空間。
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。
適用於
ManagementEventWatcher(String)
當以字串形式指定 WMI「事件查詢」時,初始化 ManagementEventWatcher 類別的新執行個體。
public:
ManagementEventWatcher(System::String ^ query);
public ManagementEventWatcher (string query);
new System.Management.ManagementEventWatcher : string -> System.Management.ManagementEventWatcher
Public Sub New (query As String)
參數
- query
- String
WMI 事件查詢,會定義監看員將接聽的事件。
範例
下列範例顯示當建立 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
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;
}
}
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
備註
監看員要接聽事件的命名空間是目前設定的預設命名空間。
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。
適用於
ManagementEventWatcher(ManagementScope, EventQuery)
初始化 ManagementEventWatcher 類別的新執行個體,此類別會接聽與指定的 WMI「事件查詢」相符的事件。
public:
ManagementEventWatcher(System::Management::ManagementScope ^ scope, System::Management::EventQuery ^ query);
public ManagementEventWatcher (System.Management.ManagementScope scope, System.Management.EventQuery query);
new System.Management.ManagementEventWatcher : System.Management.ManagementScope * System.Management.EventQuery -> System.Management.ManagementEventWatcher
Public Sub New (scope As ManagementScope, query As EventQuery)
參數
- scope
- ManagementScope
ManagementScope,表示監看員將會在其中接聽事件的範圍 (命名空間)。
- query
- EventQuery
EventQuery,表示會判斷監看員將接聽之事件的 WMI 事件查詢。
範例
在此程式代碼範例中,用戶端會在建立 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
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;
}
}
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
備註
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。
適用於
ManagementEventWatcher(String, String)
初始化 ManagementEventWatcher 類別的新執行個體,此類別會接聽與指定的 WMI「事件查詢」相符的事件。 這個變數的查詢和範圍會指定為字串。
public:
ManagementEventWatcher(System::String ^ scope, System::String ^ query);
public ManagementEventWatcher (string scope, string query);
new System.Management.ManagementEventWatcher : string * string -> System.Management.ManagementEventWatcher
Public Sub New (scope As String, query As String)
參數
- scope
- String
監看員要在其中接聽事件的管理範圍 (命名空間)。
- query
- String
查詢,定義監看員將接聽的事件。
範例
下列範例顯示當建立 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
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;
}
}
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
備註
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。
適用於
ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions)
根據指定的選項,初始化 ManagementEventWatcher 類別 (會接聽與指定的 WMI 事件查詢相符的事件) 的新執行個體。 這個變數的查詢和範圍是指定的物件。 選項物件可以指定選項 (例如,逾時和內容資訊)。
public:
ManagementEventWatcher(System::Management::ManagementScope ^ scope, System::Management::EventQuery ^ query, System::Management::EventWatcherOptions ^ options);
public ManagementEventWatcher (System.Management.ManagementScope scope, System.Management.EventQuery query, System.Management.EventWatcherOptions options);
new System.Management.ManagementEventWatcher : System.Management.ManagementScope * System.Management.EventQuery * System.Management.EventWatcherOptions -> System.Management.ManagementEventWatcher
Public Sub New (scope As ManagementScope, query As EventQuery, options As EventWatcherOptions)
參數
- scope
- ManagementScope
ManagementScope,表示監看員將會在其中接聽事件的範圍 (命名空間)。
- query
- EventQuery
EventQuery,表示會判斷監看員將接聽之事件的 WMI 事件查詢。
- options
- EventWatcherOptions
EventWatcherOptions,表示用來監看事件的額外選項。
範例
下列範例顯示當建立 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
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;
}
}
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
備註
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。
適用於
ManagementEventWatcher(String, String, EventWatcherOptions)
根據指定的選項,初始化 類別的新實例 ManagementEventWatcher ,以接聽符合指定WMI 事件查詢的事件。 這個變數的查詢和範圍會指定為字串。 選項物件可以指定選項 (例如,逾時和內容資訊)。
public:
ManagementEventWatcher(System::String ^ scope, System::String ^ query, System::Management::EventWatcherOptions ^ options);
public ManagementEventWatcher (string scope, string query, System.Management.EventWatcherOptions options);
new System.Management.ManagementEventWatcher : string * string * System.Management.EventWatcherOptions -> System.Management.ManagementEventWatcher
Public Sub New (scope As String, query As String, options As EventWatcherOptions)
參數
- scope
- String
監看員要在其中接聽事件的管理範圍 (命名空間)。
- query
- String
查詢,定義監看員將接聽的事件。
- options
- EventWatcherOptions
EventWatcherOptions,表示用來監看事件的額外選項。
範例
下列範例顯示當建立 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
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;
}
}
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
備註
.NET Framework 安全性
完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫。