EventQuery Constructores

Definición

Inicializa una nueva instancia de la clase EventQuery.

Sobrecargas

EventQuery()

Inicializa una nueva instancia de la clase EventQuery. Este es el constructor sin parámetros.

EventQuery(String)

Inicializa una nueva instancia de la clase EventQuery para la consulta especificada.

EventQuery(String, String)

Inicializa una nueva instancia de la clase EventQuery para el lenguaje y la consulta especificados.

EventQuery()

Source:
ManagementQuery.cs
Source:
ManagementQuery.cs
Source:
ManagementQuery.cs

Inicializa una nueva instancia de la clase EventQuery. Este es el constructor sin parámetros.

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

Ejemplos

En el ejemplo siguiente se muestra cómo el cliente recibe una notificación cuando se crea una instancia de Win32_Process porque la clase de evento es __InstanceCreationEvent. Para obtener más información, consulte la documentación de Instrumental de administración de Windows . El cliente recibe eventos de forma sincrónica tras llamar al método WaitForNextEvent. Para probar este ejemplo, inicie un proceso, como Bloc de notas, mientras se ejecuta el código de ejemplo.

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

Comentarios

Seguridad de .NET Framework

Plena confianza para el llamador inmediato. Un código de confianza parcial no puede utilizar este miembro. Para obtener más información, consulte Uso de bibliotecas de código de confianza parcial.

Se aplica a

EventQuery(String)

Source:
ManagementQuery.cs
Source:
ManagementQuery.cs
Source:
ManagementQuery.cs

Inicializa una nueva instancia de la clase EventQuery para la consulta especificada.

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

Parámetros

query
String

Representación textual de event query.

Ejemplos

En el ejemplo siguiente se muestra cómo el cliente recibe una notificación cuando se crea una instancia de Win32_Process porque la clase de evento es __InstanceCreationEvent. Para obtener más información, consulte la documentación de Instrumental de administración de Windows . El cliente recibe eventos de forma sincrónica tras llamar al método WaitForNextEvent. Para probar este ejemplo, inicie un proceso, como Bloc de notas, mientras se ejecuta el código de ejemplo.

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

Comentarios

Seguridad de .NET Framework

Plena confianza para el llamador inmediato. Un código de confianza parcial no puede utilizar este miembro. Para obtener más información, consulte Uso de bibliotecas de código de confianza parcial.

Se aplica a

EventQuery(String, String)

Source:
ManagementQuery.cs
Source:
ManagementQuery.cs
Source:
ManagementQuery.cs

Inicializa una nueva instancia de la clase EventQuery para el lenguaje y la consulta especificados.

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)

Parámetros

language
String

Lenguaje en el que se especifica la cadena de consulta.

query
String

Representación de cadena de la consulta.

Ejemplos

En el ejemplo siguiente se muestra cómo el cliente recibe una notificación cuando se crea una instancia de Win32_Process porque la clase de evento es __InstanceCreationEvent. Para obtener más información, consulte la documentación de Instrumental de administración de Windows . El cliente recibe eventos de forma sincrónica tras llamar al método WaitForNextEvent. Para probar este ejemplo, inicie un proceso, como Bloc de notas, mientras se ejecuta el código de ejemplo.

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

Comentarios

Seguridad de .NET Framework

Plena confianza para el llamador inmediato. Un código de confianza parcial no puede utilizar este miembro. Para obtener más información, consulte Uso de bibliotecas de código de confianza parcial.

Se aplica a