In Microsoft System Center Configuration Manager 2007, 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 Microsoft.ConfigurationManagement.ManagementProvider.SmsBackgroundWorker class that provides two event handlers:

The second parameter to of the ProcessQuery method is the WQL statement for the query.

To perform an asynchronous query

  1. Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.

  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 2007 console.

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

  Copy Code
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

See Also


Send comments about this topic to Microsoft.