在 Configuration Manager中,若要使用托管 SMS 提供程序执行异步查询,请使用 ProcessQuery 方法。
ProcessQuery 方法的第一个参数是 SmsBackgroundWorker 类的实例,该类提供两个事件处理程序:
QueryProcessObjectReady。 对于查询返回的每个对象,都会调用此事件处理程序。 事件处理程序提供表示 对象的 IResultObject 对象。
QueryProcessCompleted。 查询完成后会调用此事件处理程序。 它还提供有关发生的任何错误的信息。 有关详细信息,请参阅有关错误处理的信息,请参阅如何使用托管代码处理Configuration Manager异步错误。
ProcessQuery 方法的第二个参数是查询的 WQL 语句。
执行异步查询
设置与 SMS 提供程序的连接。 有关详细信息,请参阅 SMS 提供程序基础知识。
创建 SmsBackgroundWorker 对象,并使用回调方法名称填充 QueryProcessorObjectReady 和 QueryProcessorCompleted 属性。
在步骤 1 中获取的 WqlConnectionManager 对象中,调用 QueryProcessor 对象 ProcessQuery 方法以启动异步查询。
示例
以下示例查询所有可用的 SMS_Collection 对象,在事件处理程序中,该示例将多个集合属性写入 Configuration Manager 控制台。
有关调用示例代码的信息,请参阅调用Configuration Manager代码片段。
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...");
}
此示例方法具有以下参数:
参数 | 类型 | 说明 |
---|---|---|
connection |
管理: WqlConnectionManager |
与 SMS 提供程序的有效连接。 |
编译代码
命名空间
System
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
Assembly
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
可靠编程
可以引发Configuration Manager异常是 SmsConnectionException 和 SmsQueryException。 这些可与 SmsException 一起捕获。
另请参阅
对象概述Configuration Manager延迟属性如何使用托管代码调用Configuration Manager对象类方法如何使用托管代码连接到Configuration Manager提供程序如何创建使用托管代码Configuration Manager对象如何使用托管代码修改Configuration Manager对象如何使用托管代码执行同步Configuration Manager查询如何读取Configuration Manager使用托管代码的对象如何使用托管代码读取延迟属性如何使用托管代码执行同步Configuration Manager查询Configuration Manager扩展 WMI 查询语言Configuration Manager结果集Configuration Manager关于查询的特殊查询