EventQuery Klasa

Definicja

Reprezentuje zapytanie o zdarzenie WMI.

public ref class EventQuery : System::Management::ManagementQuery
public class EventQuery : System.Management.ManagementQuery
type EventQuery = class
    inherit ManagementQuery
Public Class EventQuery
Inherits ManagementQuery
Dziedziczenie
EventQuery
Pochodne

Przykłady

W poniższym przykładzie pokazano, jak klient odbiera powiadomienie po utworzeniu wystąpienia Win32_Process , ponieważ klasa zdarzeń jest __InstanceCreationEvent. Aby uzyskać więcej informacji, zobacz dokumentację instrumentacji zarządzania windows . Klient odbiera zdarzenia synchronicznie przez wywołanie WaitForNextEvent metody . Ten przykład można przetestować, uruchamiając proces, taki jak Notatnik, podczas gdy przykładowy kod jest uruchomiony.

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

Konstruktory

EventQuery()

Inicjuje nowe wystąpienie klasy EventQuery. Jest to konstruktor bez parametrów.

EventQuery(String)

Inicjuje EventQuery nowe wystąpienie klasy dla określonego zapytania.

EventQuery(String, String)

Inicjuje nowe wystąpienie klasy dla określonego EventQuery języka i zapytania.

Właściwości

QueryLanguage

Pobiera lub ustawia język zapytań używany w ciągu zapytania, definiując format ciągu zapytania.

(Odziedziczone po ManagementQuery)
QueryString

Pobiera lub ustawia zapytanie w formacie tekstowym.

(Odziedziczone po ManagementQuery)

Metody

Clone()

Zwraca kopię obiektu.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ParseQuery(String)

Analizuje ciąg zapytania i ustawia odpowiednio wartości właściwości. Jeśli zapytanie jest prawidłowe, właściwość i właściwość warunku klasy zapytania zostaną przeanalizowane.

(Odziedziczone po ManagementQuery)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy