How to Perform an Asynchronous Configuration Manager Query by Using Managed Code

In Configuration Manager, to perform an asynchronous query by using the managed SMS Provider, you use the ProcessQuery method.

The first parameter of the ProcessQuery method is an instance of the SmsBackgroundWorker class that provides two event handlers:

To perform an asynchronous query

  1. Set up a connection to the SMS Provider. For more information, see SMS Provider fundamentals.

  2. Create the SmsBackgroundWorker object and populate the QueryProcessorObjectReady and QueryProcessorCompleted properties with the callback method names.

  3. From the WqlConnectionManager object you obtain in step one, call the QueryProcessor object ProcessQuery method to start the asynchronous query.

Example

The following example queries for all available SMS_Collection objects, and in the event handler, the example writes several of the collection properties to the Configuration Manager console.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

public void QueryCollections(WqlConnectionManager connection)  
{  
    try  
    {  
        // Set up the query.  
        SmsBackgroundWorker bw1 = new SmsBackgroundWorker();  
        bw1.QueryProcessorObjectReady += new EventHandler<QueryProcessorObjectEventArgs>(bw1_QueryProcessorObjectReady);  
        bw1.QueryProcessorCompleted += new EventHandler<RunWorkerCompletedEventArgs>(bw1_QueryProcessorCompleted);  

        // Query for all collections.  
        connection.QueryProcessor.ProcessQuery(bw1, "select * from SMS_Collection");  

        // Pause while query runs.  
        Console.ReadLine();  
    }  
    catch (SmsException ex)  
    {  
        Console.WriteLine("Failed to start asynchronous query: ", ex.Message);  
    }  
}  

void bw1_QueryProcessorObjectReady(object sender, QueryProcessorObjectEventArgs e)  
{  
    try  
    {  
        // Get the collection.  
        IResultObject collection = (IResultObject)e.ResultObject;  

        //Display properties.  
        Console.WriteLine(collection["CollectionID"].StringValue);  
        Console.WriteLine(collection["Name"].StringValue);  
        Console.WriteLine();  
        collection.Dispose();  
    }  
    catch (SmsQueryException eX)  
    {  
        Console.WriteLine("Query Error: " + eX.Message);  
    }  
}  

void bw1_QueryProcessorCompleted(object sender, RunWorkerCompletedEventArgs e)  
{  
    Console.WriteLine("Done...");  
}  

This example method has the following parameters:

Parameter Type Description
connection Managed: WqlConnectionManager A valid connection to the SMS Provider.

Compiling the Code

Namespaces

System

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

Robust Programming

The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.

See Also

Objects overview Configuration Manager Lazy Properties
How to Call a Configuration Manager Object Class Method by Using Managed Code
How to Connect to a Configuration Manager Provider using Managed Code
How to Create a Configuration Manager Object by Using Managed Code
How to Modify a Configuration Manager Object by Using Managed Code
How to Perform a Synchronous Configuration Manager Query by Using Managed Code
How to Read a Configuration Manager Object by Using Managed Code
How to Read Lazy Properties by Using Managed Code
How to Perform a Synchronous Configuration Manager Query Using Managed Code
Configuration Manager Extended WMI Query Language
Configuration Manager Result Sets
Configuration Manager Special Queries
About queries