Browse DevX
Sign up for e-mail newsletters from DevX


Orchestrate an Efficient .NET Network Using the WMI Event Model  : Page 3

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

Here's the simple WQL event query used in the sample project:

string queryString = @"SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Processor' AND TargetInstance.LoadPercentage > 15";

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



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


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


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.


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:

  1. Right click the "Management Events" node, and choose "Add Event Query."
  2. Open Root\ CIMV2, and locate the "Processors" node.
  3. Choose a polling interval.
  4. Click on the "Advanced" button. Here you can specify criteria for your where clause, and you can see the query that's being written for you. It should be exactly the same as the query above.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date