Article 10/10/2022
7 contributors
Feedback
In this article
Example
Compiling the Code
Robust Programming
See Also
To perform an asynchronous query on a 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.
Set up a connection to the 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 your application still runs while the query is run.
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;
}
This example method has the following parameters:
Expand table
Parameter
Type
Description
Scope
ManagementScope
A valid ManagementScope
. The path should be root\ccm.
System.
System.Management.
System.Management.
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 Using System.Management