How to Perform an Asynchronous Query by Using System.Management
Updated: November 1, 2013
Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager
To perform an asynchronous query on a System Center 2012 R2 Configuration Manager client Windows Instrumentation (WMI) namespace, create a ManagementObjectSearcher object that specifies a WQL query. You then create a ManagementOperationObserver that specifies an event handler for each query result and also for the end of the query.
The asynchronous query is run when the ManagementObjectSearcher object Get method is called with the ManagementOperationObserver object.
To perform an asynchronous query
Set up a connection to the System Center 2012 R2 Configuration Manager client WMI namespace. For more information, see How to Connect to the Configuration Manager Client WMI Namespace by Using System.Management.
Create a ManagementObjectSearcher object.
Create a ManagementOperationObserver object.
Add an ObjectReadyEventHandler method the ManagementOperationObserver object.
Add a CompletedEventHandler method to the ManagementOperationObserver.
Call the ManagementObjectSearcher object Get method and supply the ManagmentOperationObserver object as a parameter.
Ensure you application still runs while the query is run.
Example
The following C# code example asynchronously queries for components that are installed on a client.
For information about calling the sample code, see How to Call a WMI Class Method by Using System.Management.
public void EnumerateInstancesAsync(ManagementScope scope) { try { // Instantiate an object searcher with the query. ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new SelectQuery("CCM_InstalledComponent")); // Create a results watcher object // and handler for results and completion. ManagementOperationObserver results = new ManagementOperationObserver(); // Attach handler to events for results and completion. results.ObjectReady += new ObjectReadyEventHandler(this.NewObject); results.Completed += new CompletedEventHandler(this.Done); Console.WriteLine("Installed Components"); Console.WriteLine("--------------------"); Console.WriteLine(); // Call the asynchronous overload of Get() // to start the enumeration. searcher.Get(results); // Do something else while results // arrive asynchronously. while (!this.Completed) { System.Threading.Thread.Sleep(1000); } this.Reset(); } catch (ManagementException e) { Console.WriteLine("Failed to run query: " + e.Message); throw; } } private bool isCompleted = false; private void NewObject(object sender, ObjectReadyEventArgs obj) { try { Console.WriteLine("Name: {0}, Version = {1}", obj.NewObject["DisplayName"], obj.NewObject["Version"]); } catch (ManagementException e) { Console.WriteLine("Error: " + e.Message); } } private bool Completed { get { return isCompleted; } } private void Reset() { isCompleted = false; } private void Done(object sender, CompletedEventArgs obj) { isCompleted = true; }
Compiling the Code
System.
System.Management.
System.Management.
Robust Programming
The exception that can be raised is System.Management.ManagementException.
About Configuration Manager WMI Programming
How to Call a WMI Class Method by Using System.Management
How to Connect to the Configuration Manager Client WMI Namespace by Using System.Management
How to Perform a Synchronous Query by Using System.Management
How to Read a WMI Object by Using System.Management
This example method has the following parameters:
Parameter
Type
Description
Scope
ManagementScope
A valid ManagementScope. The path should be root\ccm.