Learn how WMI events can help your .NET applications listen in on a vast array of system activities and even take advantage of spare capacity.

by Eric McMullen

Sep 7, 2004

Page 3 of 4

Figure 2. New Items: After installing the WMI extensions for Visual Studio, you'll notice new items in the server explorer.

Writing the Query
Implementing WIC means subscribing to a WMI event. The first step is to learn some WMI Query Language (WQL) a syntax based on Structured Query Language (SQL). First, I'll show you how to write a query by hand, and then clue you in to an easier method.

The English-language translation goes like this: "Check the Win32_Processor LoadPercentage every second, and let me know when it falls below 15%." The following table summarizes the WQL used above.

Table 2: Introduction to WQL.

WQL Clause

Description

SELECT

Spells out
the information you want back from your event.Just like standard SQL, the * is a wildcard meaning “give me
everything you have.”

FROM

Specifies the
type of event to listen for—creation, modification, or deletion.__InstanceOperationEventis the most general, firing when
any one of the three events occurs.

WITHIN

Defines the
polling interval, in seconds.As
always, there’s a tradeoff: you can improve performance with a longer
interval, but the event will fire less frequently.

WHERE

Identifies
both the statistics to query, and the criteria to look for.

Figure 3. Subscribing to WMI Events: You can subscribe to WMI events through Visual Studio, but watch out: they steal focus from the code window.

All of this should seem a little cryptic. Where did that 'Win32_Processor' come from? How can you find out what properties it exposes? Of course it's all documented by the platform SDK here, but there's another way that's easier than pouring through the documentation.

Over a year ago, to little fanfare, Microsoft released a set of extensions for working with WMI in Visual Studio's Server Explorer. The extensions, available here, make it easier to build queries and to explore the WMI classes. Once the tools are installed, you should see two additional nodes on your Server Explorer: Management Events and Management Classes.

Figure 4. The Query Builder: Use the query builder to explore the vast WMI universe.

The WMI Extensions are pretty nice for a couple of reasons. First, you can register for notifications within the VisualStudio application. It's great for diagnostic purposes, and to get an idea of how often the event fires without stepping into the debugger.

Even better, the WMI Extensions can function as a WQL query builder. Using these tools, you can choose your events and criteria visually, and then paste the text into your code when you’re satisfied. To build the same query the easy way, follow these steps: