ManagementEventWatcher ManagementEventWatcher ManagementEventWatcher ManagementEventWatcher Constructors

Definizione

Overload

ManagementEventWatcher() ManagementEventWatcher() ManagementEventWatcher()

Inizializza una nuova istanza della classe ManagementEventWatcher.Initializes a new instance of the ManagementEventWatcher class. Per un'inizializzazione ulteriore, impostare le proprietà sull'oggetto.For further initialization, set the properties on the object. È il costruttore predefinito.This is the default constructor.

ManagementEventWatcher(EventQuery) ManagementEventWatcher(EventQuery) ManagementEventWatcher(EventQuery) ManagementEventWatcher(EventQuery)

Inizializza una nuova istanza della classe ManagementEventWatcher quando viene specificata una query di eventi WMI.Initializes a new instance of the ManagementEventWatcher class when given a WMI event query.

ManagementEventWatcher(String) ManagementEventWatcher(String) ManagementEventWatcher(String) ManagementEventWatcher(String)

Inizializza una nuova istanza della classe ManagementEventWatcher quando viene specificata una query di eventi WMI in formato stringa.Initializes a new instance of the ManagementEventWatcher class when given a WMI event query in the form of a string.

ManagementEventWatcher(ManagementScope, EventQuery) ManagementEventWatcher(ManagementScope, EventQuery) ManagementEventWatcher(ManagementScope, EventQuery) ManagementEventWatcher(ManagementScope, EventQuery)

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI data.Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query.

ManagementEventWatcher(String, String) ManagementEventWatcher(String, String) ManagementEventWatcher(String, String) ManagementEventWatcher(String, String)

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI data.Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query. Per questa variante, la query e l'ambito sono specificati in forma di stringhe.For this variant, the query and the scope are specified as strings.

ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions) ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions) ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions) 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.Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query, according to the specified options. Per questa variante, la query e l'ambito sono oggetti specificati.For this variant, the query and the scope are specified objects. L'oggetto di opzioni può specificare opzioni quali il timeout e le informazioni sul contesto.The options object can specify options such as time-out and context information.

ManagementEventWatcher(String, String, EventWatcherOptions) ManagementEventWatcher(String, String, EventWatcherOptions) ManagementEventWatcher(String, String, EventWatcherOptions) ManagementEventWatcher(String, String, EventWatcherOptions)

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI, in base alle opzioni specificate.Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query, according to the specified options. Per questa variante, la query e l'ambito sono specificati in forma di stringhe.For this variant, the query and the scope are specified as strings. L'oggetto di opzioni può specificare opzioni quali il timeout e le informazioni sul contesto.The options object can specify options such as a time-out and context information.

ManagementEventWatcher() ManagementEventWatcher() ManagementEventWatcher()

Inizializza una nuova istanza della classe ManagementEventWatcher.Initializes a new instance of the ManagementEventWatcher class. Per un'inizializzazione ulteriore, impostare le proprietà sull'oggetto.For further initialization, set the properties on the object. È il costruttore predefinito.This is the default constructor.

public:
 ManagementEventWatcher();
public ManagementEventWatcher ();
Public Sub New ()

Esempi

Nell'esempio seguente viene illustrato il client riceve una notifica quando un'istanza del Win32_Process viene creato perché è la classe di evento InstanceCreationEvent.The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Per altre informazioni, vedere la Windows Management Instrumentation documentazione.For more information, see the Windows Management Instrumentation documentation. Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent.The client receives events synchronously by calling the WaitForNextEvent method. Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.This example can be tested by starting a process, such as Notepad, while the example code is running.

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
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

Commenti

Sicurezza di .NET Framework.NET Framework Security

Attendibilità totale per il chiamante immediato.Full trust for the immediate caller. Impossibile usare questo membro in codice parzialmente attendibile.This member cannot be used by partially trusted code. Per altre informazioni, vedere utilizzo di librerie da codice parzialmente attendibile.For more information, see Using Libraries from Partially Trusted Code.

ManagementEventWatcher(EventQuery) ManagementEventWatcher(EventQuery) ManagementEventWatcher(EventQuery) ManagementEventWatcher(EventQuery)

Inizializza una nuova istanza della classe ManagementEventWatcher quando viene specificata una query di eventi WMI.Initializes a new instance of the ManagementEventWatcher class when given a WMI event query.

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 EventQuery EventQuery EventQuery

Oggetto EventQuery che rappresenta una query di eventi WMI, che determina gli eventi che saranno attesi dalla funzione di controllo.An EventQuery representing a WMI event query, which determines the events for which the watcher will listen.

Esempi

In questo esempio di codice, il client riceve una notifica quando un'istanza di Win32_Process viene creato perché è la classe di evento InstanceCreationEvent.In this code example, the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Per altre informazioni, vedere la Windows Management Instrumentation documentazione.For more information, see the Windows Management Instrumentation documentation. Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent.The client receives events synchronously by calling the WaitForNextEvent method. Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.This example can be tested by starting a process, such as Notepad, while the example code is running.

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
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

Commenti

Lo spazio dei nomi in cui il controllo attenderà gli eventi è lo spazio dei nomi predefinito che è attualmente impostato.The namespace in which the watcher will be listening for events is the default namespace that is currently set.

Sicurezza di .NET Framework.NET Framework Security

Attendibilità totale per il chiamante immediato.Full trust for the immediate caller. Impossibile usare questo membro in codice parzialmente attendibile.This member cannot be used by partially trusted code. Per altre informazioni, vedere utilizzo di librerie da codice parzialmente attendibile.For more information, see Using Libraries from Partially Trusted Code.

ManagementEventWatcher(String) ManagementEventWatcher(String) ManagementEventWatcher(String) ManagementEventWatcher(String)

Inizializza una nuova istanza della classe ManagementEventWatcher quando viene specificata una query di eventi WMI in formato stringa.Initializes a new instance of the ManagementEventWatcher class when given a WMI event query in the form of a string.

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 String String String

Query di eventi WMI che definisce gli eventi che saranno attesi dalla funzione di controllo.A WMI event query, which defines the events for which the watcher will listen.

Esempi

Nell'esempio seguente viene illustrato il client riceve una notifica quando un'istanza del Win32_Process viene creato perché è la classe di evento InstanceCreationEvent.The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Per altre informazioni, vedere la Windows Management Instrumentation documentazione.For more information, see the Windows Management Instrumentation documentation. Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent.The client receives events synchronously by calling the WaitForNextEvent method. Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.This example can be tested by starting a process, such as Notepad, while the example code is running.

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
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

Commenti

Lo spazio dei nomi in cui il controllo attenderà gli eventi è lo spazio dei nomi predefinito che è attualmente impostato.The namespace in which the watcher will be listening for events is the default namespace that is currently set.

Sicurezza di .NET Framework.NET Framework Security

Attendibilità totale per il chiamante immediato.Full trust for the immediate caller. Impossibile usare questo membro in codice parzialmente attendibile.This member cannot be used by partially trusted code. Per altre informazioni, vedere utilizzo di librerie da codice parzialmente attendibile.For more information, see Using Libraries from Partially Trusted Code.

ManagementEventWatcher(ManagementScope, EventQuery) ManagementEventWatcher(ManagementScope, EventQuery) ManagementEventWatcher(ManagementScope, EventQuery) ManagementEventWatcher(ManagementScope, EventQuery)

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI data.Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query.

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 ManagementScope ManagementScope ManagementScope

Oggetto ManagementScope che rappresenta l'ambito (spazio dei nomi) nel quale la funzione di controllo attenderà gli eventi.A ManagementScope representing the scope (namespace) in which the watcher will listen for events.

query
EventQuery EventQuery EventQuery EventQuery

Oggetto EventQuery che rappresenta una query di eventi WMI, che determina gli eventi che saranno attesi dalla funzione di controllo.An EventQuery representing a WMI event query, which determines the events for which the watcher will listen.

Esempi

In questo esempio di codice, il client riceve una notifica quando un'istanza di Win32_Process viene creato perché è la classe di evento InstanceCreationEvent.In this code example, the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Per altre informazioni, vedere la Windows Management Instrumentation documentazione.For more information, see the Windows Management Instrumentation documentation. Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent.The client receives events synchronously by calling the WaitForNextEvent method. Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.This example can be tested by starting a process, such as Notepad, while the example code is running.

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
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

Commenti

Sicurezza di .NET Framework.NET Framework Security

Attendibilità totale per il chiamante immediato.Full trust for the immediate caller. Impossibile usare questo membro in codice parzialmente attendibile.This member cannot be used by partially trusted code. Per altre informazioni, vedere utilizzo di librerie da codice parzialmente attendibile.For more information, see Using Libraries from Partially Trusted Code.

ManagementEventWatcher(String, String) ManagementEventWatcher(String, String) ManagementEventWatcher(String, String) ManagementEventWatcher(String, String)

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI data.Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query. Per questa variante, la query e l'ambito sono specificati in forma di stringhe.For this variant, the query and the scope are specified as strings.

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 String String String

Ambito di gestione, ossia spazio dei nomi, nel quale la funzione di controllo attenderà gli eventi.The management scope (namespace) in which the watcher will listen for events.

query
String String String String

Query che definisce gli eventi che saranno attesi dalla funzione di controllo.The query that defines the events for which the watcher will listen.

Esempi

Nell'esempio seguente viene illustrato il client riceve una notifica quando un'istanza del Win32_Process viene creato perché è la classe di evento InstanceCreationEvent.The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Per altre informazioni, vedere la Windows Management Instrumentation documentazione.For more information, see the Windows Management Instrumentation documentation. Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent.The client receives events synchronously by calling the WaitForNextEvent method. Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.This example can be tested by starting a process, such as Notepad, while the example code is running.

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
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

Commenti

Sicurezza di .NET Framework.NET Framework Security

Attendibilità totale per il chiamante immediato.Full trust for the immediate caller. Impossibile usare questo membro in codice parzialmente attendibile.This member cannot be used by partially trusted code. Per altre informazioni, vedere utilizzo di librerie da codice parzialmente attendibile.For more information, see Using Libraries from Partially Trusted Code.

ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions) ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions) ManagementEventWatcher(ManagementScope, EventQuery, EventWatcherOptions) 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.Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query, according to the specified options. Per questa variante, la query e l'ambito sono oggetti specificati.For this variant, the query and the scope are specified objects. L'oggetto di opzioni può specificare opzioni quali il timeout e le informazioni sul contesto.The options object can specify options such as time-out and context information.

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 ManagementScope ManagementScope ManagementScope

Oggetto ManagementScope che rappresenta l'ambito (spazio dei nomi) nel quale la funzione di controllo attenderà gli eventi.A ManagementScope representing the scope (namespace) in which the watcher will listen for events.

query
EventQuery EventQuery EventQuery EventQuery

Oggetto EventQuery che rappresenta una query di eventi WMI, che determina gli eventi che saranno attesi dalla funzione di controllo.An EventQuery representing a WMI event query, which determines the events for which the watcher will listen.

options
EventWatcherOptions EventWatcherOptions EventWatcherOptions EventWatcherOptions

Oggetto EventWatcherOptions che rappresenta le opzioni aggiuntive utilizzate per il controllo degli eventi.An EventWatcherOptions representing additional options used to watch for events.

Esempi

Nell'esempio seguente viene illustrato il client riceve una notifica quando un'istanza del Win32_Process viene creato perché è la classe di evento InstanceCreationEvent.The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Per altre informazioni, vedere la Windows Management Instrumentation documentazione.For more information, see the Windows Management Instrumentation documentation. Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent.The client receives events synchronously by calling the WaitForNextEvent method. Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.This example can be tested by starting a process, such as Notepad, while the example code is running.

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
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

Commenti

Sicurezza di .NET Framework.NET Framework Security

Attendibilità totale per il chiamante immediato.Full trust for the immediate caller. Impossibile usare questo membro in codice parzialmente attendibile.This member cannot be used by partially trusted code. Per altre informazioni, vedere utilizzo di librerie da codice parzialmente attendibile.For more information, see Using Libraries from Partially Trusted Code.

ManagementEventWatcher(String, String, EventWatcherOptions) ManagementEventWatcher(String, String, EventWatcherOptions) ManagementEventWatcher(String, String, EventWatcherOptions) ManagementEventWatcher(String, String, EventWatcherOptions)

Inizializza una nuova istanza della classe ManagementEventWatcher che attende gli eventi in conformità alla query di eventi WMI, in base alle opzioni specificate.Initializes a new instance of the ManagementEventWatcher class that listens for events conforming to the given WMI event query, according to the specified options. Per questa variante, la query e l'ambito sono specificati in forma di stringhe.For this variant, the query and the scope are specified as strings. L'oggetto di opzioni può specificare opzioni quali il timeout e le informazioni sul contesto.The options object can specify options such as a time-out and context information.

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 String String String

Ambito di gestione, ossia spazio dei nomi, nel quale la funzione di controllo attenderà gli eventi.The management scope (namespace) in which the watcher will listen for events.

query
String String String String

Query che definisce gli eventi che saranno attesi dalla funzione di controllo.The query that defines the events for which the watcher will listen.

options
EventWatcherOptions EventWatcherOptions EventWatcherOptions EventWatcherOptions

Oggetto EventWatcherOptions che rappresenta le opzioni aggiuntive utilizzate per il controllo degli eventi.An EventWatcherOptions representing additional options used to watch for events.

Esempi

Nell'esempio seguente viene illustrato il client riceve una notifica quando un'istanza del Win32_Process viene creato perché è la classe di evento InstanceCreationEvent.The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Per altre informazioni, vedere la Windows Management Instrumentation documentazione.For more information, see the Windows Management Instrumentation documentation. Il client riceve gli eventi in modo sincrono chiamando il metodo WaitForNextEvent.The client receives events synchronously by calling the WaitForNextEvent method. Questo esempio può essere testato avviando un processo, ad esempio Blocco note, mentre il codice di esempio è in esecuzione.This example can be tested by starting a process, such as Notepad, while the example code is running.

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
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

Commenti

Sicurezza di .NET Framework.NET Framework Security

Attendibilità totale per il chiamante immediato.Full trust for the immediate caller. Impossibile usare questo membro in codice parzialmente attendibile.This member cannot be used by partially trusted code. Per altre informazioni, vedere utilizzo di librerie da codice parzialmente attendibile.For more information, see Using Libraries from Partially Trusted Code.

Si applica a