EventQuery 类

定义

表示一个 WMI 事件查询。Represents a WMI event query.

public ref class EventQuery : System::Management::ManagementQuery
public class EventQuery : System.Management.ManagementQuery
type EventQuery = class
    inherit ManagementQuery
Public Class EventQuery
Inherits ManagementQuery
继承
EventQuery
派生

示例

下面的示例演示在创建Win32_Process的实例时客户端如何收到通知, 因为事件类为 __InstanceCreationEventThe following example shows how the client receives notification when an instance of Win32_Process is created because the event class is __InstanceCreationEvent. 有关详细信息, 请参阅Windows Management Instrumentation文档。For more information, see the Windows Management Instrumentation documentation. 客户端通过调用 WaitForNextEvent 方法来同步接收事件。The client receives events synchronously by calling the WaitForNextEvent method. 在运行此代码示例的同时,可以启动进程(如记事本)来测试此示例。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
        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

构造函数

EventQuery()

初始化 EventQuery 类的新实例。Initializes a new instance of the EventQuery class. 这是无参数构造函数。This is the parameterless constructor.

EventQuery(String)

为指定的查询初始化 EventQuery 类的新实例。Initializes a new instance of the EventQuery class for the specified query.

EventQuery(String, String)

根据指定的语言和查询初始化 EventQuery 类的新实例。Initializes a new instance of the EventQuery class for the specified language and query.

属性

QueryLanguage

获取或设置查询字符串中使用的查询语言,该语言定义查询字符串的格式。Gets or sets the query language used in the query string, defining the format of the query string.

(继承自 ManagementQuery)
QueryString

以文本格式获取或设置查询。Gets or sets the query in text format.

(继承自 ManagementQuery)

方法

Clone()

返回对象的一个副本。Returns a copy of the object.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ParseQuery(String)

分析查询字符串并相应地设置属性值。Parses the query string and sets the property values accordingly. 如果查询有效,将分析查询的类名属性和条件属性。If the query is valid, the class name property and condition property of the query will be parsed.

(继承自 ManagementQuery)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于