ManagementEventWatcher.WaitForNextEvent Método

Definición

Espera al siguiente evento que coincida con la consulta especificada que va a llegar, y lo devuelve.Waits for the next event that matches the specified query to arrive, and then returns it.

public:
 System::Management::ManagementBaseObject ^ WaitForNextEvent();
public System.Management.ManagementBaseObject WaitForNextEvent ();
member this.WaitForNextEvent : unit -> System.Management.ManagementBaseObject
Public Function WaitForNextEvent () As ManagementBaseObject

Devoluciones

ManagementBaseObject

ManagementBaseObject que representa el evento recién llegado.A ManagementBaseObject representing the newly arrived event.

Ejemplos

En el ejemplo siguiente se muestra cómo el cliente recibe la notificación cuando se crea una instancia de Win32_Process porque se __InstanceCreationEvent la clase de eventos.The following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. Para obtener más información, consulte la documentación de instrumental de administración de Windows .For more information, see the Windows Management Instrumentation documentation. El cliente recibe eventos de forma sincrónica tras llamar al método WaitForNextEvent.The client receives events synchronously by calling the WaitForNextEvent method. Para probar este ejemplo, inicie un proceso, como Bloc de notas, mientras se ejecuta el código de ejemplo.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.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

Comentarios

Si el objeto de monitor de eventos contiene opciones con un tiempo de espera especificado, la API esperará el siguiente evento solo durante el período de tiempo especificado; de lo contrario, la API se bloqueará hasta que se produzca el siguiente evento.If the event watcher object contains options with a specified time-out, the API will wait for the next event only for the specified amount of time; otherwise, the API will be blocked until the next event occurs.

Seguridad de .NET Framework.NET Framework Security

Plena confianza para el llamador inmediato.Full trust for the immediate caller. Un código de confianza parcial no puede utilizar este miembro.This member cannot be used by partially trusted code. Para obtener más información, vea usar bibliotecas desde código de confianza parcial.For more information, see Using Libraries from Partially Trusted Code.

Se aplica a