Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

WqlEventQuery Class

Represents a WMI event query in WQL format.

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

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

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

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.

Example

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

// This sample demonstrates how to subscribe to an event
// using a WQL event query.

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
        WqlEventQuery eventQuery = new WqlEventQuery("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();

        return 0;
    }

    public class MyHandler
    {
        private bool isArrived = false;
 
        //Handles the event when it arrives
        public void Arrived(object sender, EventArrivedEventArgs e) {
            ManagementBaseObject eventArg = (ManagementBaseObject)(e.NewEvent["TargetClass"]);
            Console.WriteLine("Class Deleted = " + eventArg["__CLASS"]);
            isArrived = true;
        }
 
         //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 a WQL event query.

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 WqlEventQuery("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()

    End Sub

    Public Class MyHandler
        Private _isArrived As Boolean = False
 
        'Handles the event when it arrives
        Public Sub Arrived(sender As Object, e As EventArrivedEventArgs)
            Dim eventArg As ManagementBaseObject = CType( _
                e.NewEvent("TargetClass"), _
                ManagementBaseObject)
            Console.WriteLine(("Class Deleted = " + eventArg("__CLASS")))
            _isArrived = True
        End Sub

        '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

WqlEventQuery Members | System.Management Namespace

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