ManagementEventWatcher Costruttori

Definizione

Inizializza una nuova istanza della classe ManagementEventWatcher.

Overload

ManagementEventWatcher()

Inizializza una nuova istanza della classe ManagementEventWatcher. Per un'inizializzazione ulteriore, impostare le proprietà sull'oggetto. Si tratta del costruttore senza parametri.

ManagementEventWatcher(EventQuery)

Inizializza una nuova istanza della classe ManagementEventWatcher quando viene specificata una query di eventi WMI.

ManagementEventWatcher(String)

Inizializza una nuova istanza della classe ManagementEventWatcher quando viene specificata una query di eventi WMI in formato stringa.

ManagementEventWatcher(ManagementScope, EventQuery)

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI data.

ManagementEventWatcher(String, String)

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI data. Per questa variante, la query e l'ambito sono specificati in forma di stringhe.

ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions)

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI fornita, in base alle opzioni specificate. Per questa variante, la query e l'ambito sono oggetti specificati. L'oggetto di opzioni può specificare opzioni quali il timeout e le informazioni sul contesto.

ManagementEventWatcher(String, String, EventWatcherOptions)

Inizializza una nuova istanza della ManagementEventWatcher classe che ascolta gli eventi conformi alla query di evento WMI specificata, in base alle opzioni specificate. Per questa variante, la query e l'ambito sono specificati in forma di stringhe. L'oggetto di opzioni può specificare opzioni quali il timeout e le informazioni sul contesto.

ManagementEventWatcher()

Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs

Inizializza una nuova istanza della classe ManagementEventWatcher. Per un'inizializzazione ulteriore, impostare le proprietà sull'oggetto. Si tratta del costruttore senza parametri.

public:
 ManagementEventWatcher();
public ManagementEventWatcher ();
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 di Strumentazione gestione Windows . Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent. 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
        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

Commenti

Sicurezza di .NET Framework

Attendibilità totale per il chiamante immediato. Impossibile utilizzare questo membro in codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.

Si applica a

ManagementEventWatcher(EventQuery)

Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs

Inizializza una nuova istanza della classe ManagementEventWatcher quando viene specificata una query di eventi WMI.

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)

Parametri

query
EventQuery

Oggetto EventQuery che rappresenta una query di eventi WMI, che determina gli eventi che saranno attesi dalla funzione di controllo.

Esempio

In questo esempio di codice il client riceve una notifica quando viene creata un'istanza di Win32_Process perché la classe di evento è __InstanceCreationEvent. Per altre informazioni, vedere la documentazione di Strumentazione gestione Windows . Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent. 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
        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

Commenti

Lo spazio dei nomi in cui il watcher ascolterà gli eventi è lo spazio dei nomi predefinito attualmente impostato.

Sicurezza di .NET Framework

Attendibilità totale per il chiamante immediato. Impossibile utilizzare questo membro in codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.

Si applica a

ManagementEventWatcher(String)

Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs

Inizializza una nuova istanza della classe ManagementEventWatcher quando viene specificata una query di eventi WMI in formato stringa.

public:
 ManagementEventWatcher(System::String ^ query);
public ManagementEventWatcher (string query);
new System.Management.ManagementEventWatcher : string -> System.Management.ManagementEventWatcher
Public Sub New (query As String)

Parametri

query
String

Query di eventi WMI che definisce gli eventi che saranno attesi dalla funzione di controllo.

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 di Strumentazione gestione Windows . Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent. 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
        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

Commenti

Lo spazio dei nomi in cui il watcher ascolterà gli eventi è lo spazio dei nomi predefinito attualmente impostato.

Sicurezza di .NET Framework

Attendibilità totale per il chiamante immediato. Impossibile utilizzare questo membro in codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.

Si applica a

ManagementEventWatcher(ManagementScope, EventQuery)

Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI data.

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)

Parametri

scope
ManagementScope

Oggetto ManagementScope che rappresenta l'ambito (spazio dei nomi) nel quale la funzione di controllo attenderà gli eventi.

query
EventQuery

Oggetto EventQuery che rappresenta una query di eventi WMI, che determina gli eventi che saranno attesi dalla funzione di controllo.

Esempio

In questo esempio di codice il client riceve una notifica quando viene creata un'istanza di Win32_Process perché la classe di evento è __InstanceCreationEvent. Per altre informazioni, vedere la documentazione di Strumentazione gestione Windows . Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent. 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
        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

Commenti

Sicurezza di .NET Framework

Attendibilità totale per il chiamante immediato. Impossibile utilizzare questo membro in codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.

Si applica a

ManagementEventWatcher(String, String)

Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI data. Per questa variante, la query e l'ambito sono specificati in forma di stringhe.

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)

Parametri

scope
String

Ambito di gestione, ossia spazio dei nomi, nel quale la funzione di controllo attenderà gli eventi.

query
String

Query che definisce gli eventi che saranno attesi dalla funzione di controllo.

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 di Strumentazione gestione Windows . Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent. 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
        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

Commenti

Sicurezza di .NET Framework

Attendibilità totale per il chiamante immediato. Impossibile utilizzare questo membro in codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.

Si applica a

ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions)

Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI fornita, in base alle opzioni specificate. Per questa variante, la query e l'ambito sono oggetti specificati. L'oggetto di opzioni può specificare opzioni quali il timeout e le informazioni sul contesto.

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)

Parametri

scope
ManagementScope

Oggetto ManagementScope che rappresenta l'ambito (spazio dei nomi) nel quale la funzione di controllo attenderà gli eventi.

query
EventQuery

Oggetto EventQuery che rappresenta una query di eventi WMI, che determina gli eventi che saranno attesi dalla funzione di controllo.

options
EventWatcherOptions

Oggetto EventWatcherOptions che rappresenta le opzioni aggiuntive utilizzate per il controllo degli eventi.

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 di Strumentazione gestione Windows . Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent. 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
        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

Commenti

Sicurezza di .NET Framework

Attendibilità totale per il chiamante immediato. Impossibile utilizzare questo membro in codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.

Si applica a

ManagementEventWatcher(String, String, EventWatcherOptions)

Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs
Origine:
ManagementEventWatcher.cs

Inizializza una nuova istanza della ManagementEventWatcher classe che ascolta gli eventi conformi alla query di evento WMI specificata, in base alle opzioni specificate. Per questa variante, la query e l'ambito sono specificati in forma di stringhe. L'oggetto di opzioni può specificare opzioni quali il timeout e le informazioni sul contesto.

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)

Parametri

scope
String

Ambito di gestione, ossia spazio dei nomi, nel quale la funzione di controllo attenderà gli eventi.

query
String

Query che definisce gli eventi che saranno attesi dalla funzione di controllo.

options
EventWatcherOptions

Oggetto EventWatcherOptions che rappresenta le opzioni aggiuntive utilizzate per il controllo degli eventi.

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 di Strumentazione gestione Windows . Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent. 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
        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

Commenti

Sicurezza di .NET Framework

Attendibilità totale per il chiamante immediato. Impossibile utilizzare questo membro in codice parzialmente attendibile. Per altre informazioni, vedere Uso di librerie da codice parzialmente attendibile.

Si applica a