Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

EventQuery Class

Represents a WMI event query.

For a list of all members of this type, see EventQuery Members.

System.Object
   System.Management.ManagementQuery
      System.Management.EventQuery
         System.Management.WqlEventQuery

[Visual Basic]
Public Class EventQuery
   Inherits ManagementQuery
[C#]
public class EventQuery : ManagementQuery
[C++]
public __gc class EventQuery : public ManagementQuery
[JScript]
public class EventQuery extends ManagementQuery

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Objects of this class or its derivatives are used in ManagementEventWatcher to subscribe to WMI events. Use more specific derivatives of this class whenever possible.

Example

[C#] 
using System;
using System.Management;

// This sample demonstrates how to subscribe to an event
// using the EventQuery object.

class Sample_EventQuery
{
    public static int Main(string[] args)
    {
        //For this example, we make sure we have an arbitrary class on root\default
        ManagementClass newClass = new ManagementClass(
            "root\\default",
            String.Empty,
            null);
        newClass["__Class"] = "TestWql";
        newClass.Put();

        //Create a query object for watching for class deletion events
        EventQuery eventQuery = new EventQuery("select * from __classdeletionevent");

        //Initialize an event watcher object with this query
        ManagementEventWatcher watcher = new ManagementEventWatcher(
            new ManagementScope("root/default"),
            eventQuery);

        //Set up a handler for incoming events
        MyHandler handler = new MyHandler();
        watcher.EventArrived += new EventArrivedEventHandler(handler.Arrived);

        //Start watching for events
        watcher.Start();

        //For this example, we delete the class to trigger an event
        newClass.Delete();

        //Nothing better to do - we loop to wait for an event to arrive.
        while (!handler.IsArrived) {
             System.Threading.Thread.Sleep(1000);
        }

        //In this example we only want to wait for one event, so we can stop watching
        watcher.Stop();

        //Get some values from the event.
        //Note: this can also be done in the event handler.
        ManagementBaseObject eventArg =
            (ManagementBaseObject)(handler.ReturnedArgs.NewEvent["TargetClass"]);
        Console.WriteLine("Class Deleted = " + eventArg["__CLASS"]);

        return 0;
    }

    public class MyHandler
    {
        private bool isArrived = false;
        private EventArrivedEventArgs args;

        //Handles the event when it arrives
        public void Arrived(object sender, EventArrivedEventArgs e) {
            args = e;
            isArrived = true;
        }
 
        //Public property to get at the event information stored in the handler
        public EventArrivedEventArgs ReturnedArgs {
            get {
                return args;
            }
        }

        //Used to determine whether the event has arrived or not.
        public bool IsArrived {
            get {
                return isArrived;
            }
        }
    }
}
   
[Visual Basic] 
Imports System
Imports System.Management

' This sample demonstrates how to subscribe an event
' using the EventQuery object.

Class Sample_EventQuery
    Public Shared Sub Main()

        'For this example, we make sure we have an arbitrary class on root\default
        Dim newClass As New ManagementClass( _
            "root\default", _
            String.Empty, Nothing)
            newClass("__Class") = "TestWql"
            newClass.Put()

        'Create a query object for watching for class deletion events
        Dim eventQuery As New EventQuery("select * from __classdeletionevent")

        'Initialize an event watcher object with this query
        Dim watcher As New ManagementEventWatcher( _
            New ManagementScope("root/default"), _
            eventQuery)

        'Set up a handler for incoming events
        Dim handler As New MyHandler()
        AddHandler watcher.EventArrived, AddressOf handler.Arrived
   
        'Start watching for events
        watcher.Start()

        'For this example, we delete the class to trigger an event
        newClass.Delete()

        'Nothing better to do - we loop to wait for an event to arrive.
        While Not handler.IsArrived
            Console.Write("0")
            System.Threading.Thread.Sleep(1000)
        End While

        'In this example we only want to wait for one event, so we can stop watching
        watcher.Stop()

        'Get some values from the event
        'Note: this can also be done in the event handler.
        Dim eventArg As ManagementBaseObject = CType( _
            handler.ReturnedArgs.NewEvent("TargetClass"), _
            ManagementBaseObject)
        Console.WriteLine(("Class Deleted = " + eventArg("__CLASS")))

    End Sub

    Public Class MyHandler
        Private _isArrived As Boolean = False
        Private args As EventArrivedEventArgs

        'Handles the event when it arrives
        Public Sub Arrived(sender As Object, e As EventArrivedEventArgs)
            args = e
            _isArrived = True
        End Sub

        'Public property to get at the event information stored in the handler         
        Public ReadOnly Property ReturnedArgs() As EventArrivedEventArgs
            Get
                Return args
            End Get
        End Property

        'Used to determine whether the event has arrived or not.
        Public ReadOnly Property IsArrived() As Boolean
            Get
                Return _isArrived
            End Get
        End Property
    End Class
End Class
   

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Management

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Management (in System.Management.dll)

See Also

EventQuery Members | System.Management Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.