Export (0) Print
Expand All

How To: Receive Management Events Without Waiting 

.NET Framework 2.0

A client application may set up an asynchronous subscription and carry on other operations while waiting for events.

To subscribe to events asynchronously

  1. Create an event query specifying the events using either the EventQuery class or the WqlEventQuery class.

  2. Create a new ManagementEventWatcher instance to subscribe to events matching the query.

  3. Set up a listener for events using the EventArrived and the Stopped events of the ManagementEventWatcher class.

  4. Create an event handler class with a method that receives the EventArrivedEventArgs class.

  5. Create an instance of the EventArrivedEventHandler delegate that uses the event handler method.

  6. Start listening for the events by calling the Start method of the ManagementEventWatcher class.

    This is the same operation as calling IWbemServices::ExecNotificationQueryAsync or SWbemServices::ExecNotificationQueryAsync in unmanaged WMI. For more information see, "IWbemServices::ExecNotificationQueryAsync" and "SWbemServices::ExecNotificationQueryAsync" in the Windows Management Instrumentation documentation in the MSDN Library at http://msdn.microsoft.com/library.

  7. Perform other operations.

  8. Call the Stop method of the ManagementEventWatcher class to stop listening.

    This call terminates the subscription.

Example

The following asynchronous code example sets up a WMI timer to raise an event every second, and removes it when it is no longer needed. The ManagementEventWatcher object defines several .NET Framework events, which are raised when WMI events are delivered. Delegates are attached to these events for handling the incoming data.

using System;
using System.Management;

// This example shows asynchronous consumption of events.
// In this example you are listening for timer events.
// The first part of the example sets up the timer.
public class EventWatcherAsync 
{
    public EventWatcherAsync() 
    {
        // Set up a timer to raise events every 1 second
        //=============================================
        ManagementClass timerClass = 
            new ManagementClass("__IntervalTimerInstruction");
        ManagementObject timer = timerClass.CreateInstance();
        timer["TimerId"] = "Timer1";
        timer["IntervalBetweenEvents"] = 1000;
        timer.Put();

        // Set up the event consumer
        //==========================
        // Create event query to receive timer events
        WqlEventQuery query = 
            new WqlEventQuery("__TimerEvent",
            "TimerId=\"Timer1\"");

        // Initialize an event watcher and 
        // subscribe to timer events 
        ManagementEventWatcher watcher = 
            new ManagementEventWatcher(query);

        // Set up a listener for events
        watcher.EventArrived += 
            new EventArrivedEventHandler(HandleEvent);

        // Start listening
        watcher.Start();

        // Do something in the meantime
        System.Threading.Thread.Sleep(10000);
      
        // Stop listening
        watcher.Stop();
        
    }

    private void HandleEvent(object sender,
        EventArrivedEventArgs e)   
    {
        Console.WriteLine("Event arrived !");
    }

    public static void Main()
    {
        EventWatcherAsync eventWather = new EventWatcherAsync();
    }
}

Compiling the Code

The example requires references to the System and System.Management namespaces.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft