Receiving Notifications

The final topic I will cover is writing clients that can receive notifications. The general principles are identical to those for performing asynchronous queries, but some of the classes used are a bit different. In place of the ManagementOperationObserver class is a ManagementEventWatcher class. This class doesn't only indicate the callback method, but is also responsible for sending the notification request (as a WQL string) off to the object manager. So the first thing we have to do is set up a ManagementEventWatcher instance initialized with the appropriate query string. The following code performs this initialization, with a query string that indicates the client wishes to be notified within five seconds whenever any Windows services are registered.

 string queryString = "SELECT * FROM __InstanceCreationEvent " +       "WITHIN 5 WHERE TargetInstance ISA 'Win32_Service'"; ManagementEventWatcher watcher = new ManagementEventWatcher(query); 

Next we have to indicate the handler method that will receive callbacks:

 watcher.EventArrived += new       BventArrivedEventHandler(myCallBackMethod); 

Here, the myCallBack method is a method which we will have implemented elsewhere and which has a signature corresponding to the EventArrivedEventHandler delegate (yes, Microsoft has defined yet another delegate...).

Finally, the ManagementEventWatcher.Start() method sends the notification request off to the object manager. You simply call this method, then sit back and wait (or, more likely, do some other work) while the notification responses come back on a thread-pool thread:

 watcher.Start(); 

If at some later point you want to cancel the notification request, so the object manager stops sending notifications, you call the ManagementEventWatcher.Stop() method:

 watcher.Stop(); 

Monitoring Display Settings Example

This example will demonstrate the use of WMI events. It's called MonitorDisplaySettings, and it is a short console application whose purpose is to warn you if for any reason the display settings for the screen area are changed and fall below 1024x768 pixels. When the code starts, it sets up a WQL query asking the WMI object manager to notify it if this event occurs. If the display settings do fall below this value, then a warning message is displayed. The main thread simply waits for some user input so that the application terminates when the user hits the Return key.

The code for the example looks like this. First here is the Main() method which sets up the request to be notified of events:

 static void Main() (    WqlEventQuery query = new WqlEventQuery(       "SELECT * FROM __InstanceModificationEvent " +       "WITHIN 2 WHERE TargetInstance ISA \"Win32_DisplayConfiguration\" " +       "AND TargetInstance.PelsWidth < 1024 AND PreviousInstance.PelsWidth " +       ">= 1024");    ManagementEventWatcher watcher = new ManagementEventWatcher(query);    CallbackClass callback = new CallbackClass();    watcher.EventArrived += new EventArrivedEventHandler(                                             callback.DisplayProblemCallback);    watcher.Start();    Console.WriteLine("Monitoring display settings.");    Console.WriteLine("Hit return to stop monitoring and exit.");    Console.ReadLine();    watcher.Stop(); } 

This code shouldn't need much additional explanation given the explanation of the ManagementEventWatcher class that I've just run over. Note that, although the aim is to test if the display resolution falls below 1024x768, I'm assuming that it's sufficient to only test the horizontal resolution. Virtually all monitors support only the standard screen resolutions, 800x600, 1024x768, etc., so in practice this is a safe enough assumption for our example.

Now here's the callback class, which handles events. The code below simply displays a warning message each time the object manager raises the event to indicate that the horizontal display resolution has fallen below 1024:

 public class CallbackClass {    public void DisplayProblemCallback(object sender, EventArrivedEventArgs e)    {       Console.WriteLine("Warning! Display settings have dropped " +                         "below 1024x768");    } } 

To test the example, you should just start it running. Then, while the example is running, bring up the properties window for the desktop, locate the Settings property page, and change the screen resolution to something coarser than 1024x768.

For my test, I picked the next lowest setting, 800x600. You'll find that within seconds, the console window in which the example is running will display the warning message:

click to expand

If you carry on playing around, you'll find that you can make further changes to the display settings without generating any further warnings, because the PreviousInstance.PelsWidth is now less than 1024, and so the conditions in the WQL event query string we supplied in the program are not satisfied. However, if you change the resolution back to 1024x768 (or something higher) and then back down again, you'll very quickly see another warning appear in the console window.



Advanced  .NET Programming
Advanced .NET Programming
ISBN: 1861006292
EAN: 2147483647
Year: 2002
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net