EventQuery Costruttori
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Inizializza una nuova istanza della classe EventQuery.
Overload
| Nome | Descrizione |
|---|---|
| EventQuery() |
Inizializza una nuova istanza della classe EventQuery. Si tratta del costruttore senza parametri. |
| EventQuery(String) |
Inizializza una nuova istanza della EventQuery classe per la query specificata. |
| EventQuery(String, String) |
Inizializza una nuova istanza della classe per il linguaggio e la EventQuery query specificati. |
EventQuery()
Inizializza una nuova istanza della classe EventQuery. Si tratta del costruttore senza parametri.
public:
EventQuery();
public EventQuery();
Public Sub New ()
Esempio
Nell'esempio seguente viene illustrato come il client riceve la notifica quando viene creata un'istanza di Win32_Process perché la classe di evento è __InstanceCreationEvent. Per altre informazioni, vedere la documentazione Windows Management Instrumentation. Il client riceve gli eventi in modo sincrono chiamando il WaitForNextEvent metodo . Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.
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
EventQuery query = new EventQuery();
query.QueryString = "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 New EventQuery
query.QueryString = "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, 50)
' 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
Commenti
Sicurezza di .NET Framework
Attendibilità totale per il chiamante immediato. Questo membro non può essere utilizzato da codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.
Si applica a
EventQuery(String)
Inizializza una nuova istanza della EventQuery classe per la query specificata.
public:
EventQuery(System::String ^ query);
public EventQuery(string query);
new System.Management.EventQuery : string -> System.Management.EventQuery
Public Sub New (query As String)
Parametri
- query
- String
Rappresentazione testuale di event query.
Esempio
Nell'esempio seguente viene illustrato come il client riceve la notifica quando viene creata un'istanza di Win32_Process perché la classe di evento è __InstanceCreationEvent. Per altre informazioni, vedere la documentazione Windows Management Instrumentation. Il client riceve gli eventi in modo sincrono chiamando il WaitForNextEvent metodo . Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.
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
EventQuery query = new EventQuery(
"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 New EventQuery( _
"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, 50)
' 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
Commenti
Sicurezza di .NET Framework
Attendibilità totale per il chiamante immediato. Questo membro non può essere utilizzato da codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.
Si applica a
EventQuery(String, String)
Inizializza una nuova istanza della classe per il linguaggio e la EventQuery query specificati.
public:
EventQuery(System::String ^ language, System::String ^ query);
public EventQuery(string language, string query);
new System.Management.EventQuery : string * string -> System.Management.EventQuery
Public Sub New (language As String, query As String)
Parametri
- language
- String
Linguaggio in cui viene specificata la stringa di query.
- query
- String
Rappresentazione di stringa della query.
Esempio
Nell'esempio seguente viene illustrato come il client riceve la notifica quando viene creata un'istanza di Win32_Process perché la classe di evento è __InstanceCreationEvent. Per altre informazioni, vedere la documentazione Windows Management Instrumentation. Il client riceve gli eventi in modo sincrono chiamando il WaitForNextEvent metodo . Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.
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
EventQuery query = new EventQuery("WQL",
"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 New EventQuery("WQL", _
"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, 50)
' 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
Commenti
Sicurezza di .NET Framework
Attendibilità totale per il chiamante immediato. Questo membro non può essere utilizzato da codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.