在 Configuration Manager 中,若要使用托管 SMS 提供程序执行异步查询,请使用 ProcessQuery 方法。
ProcessQuery 方法的第一个参数是 SmsBackgroundWorker 类的实例,该类提供两个事件处理程序:
QueryProcessObjectReady. 将为查询返回的每个对象调用此事件处理程序。 事件处理程序提供表示对象的 IResultObject 对象。
QueryProcessCompleted. 查询完成时调用此事件处理程序。 它还提供有关发生的任何错误的信息。 有关详细信息,请参阅 有关错误处理的信息,请参阅如何使用托管代码处理 Configuration Manager 异步错误。
ProcessQuery 方法的第二个参数是查询的 WQL 语句。
若要执行异步查询
建立与 SMS 提供程序的连接。 有关详细信息,请参阅 SMS 提供程序基础知识。
创建 SmsBackgroundWorker 对象,并使用回调方法名称填充 QueryProcessorObjectReady 和 QueryProcessorCompleted 属性。
从第一步中获取的 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 特殊查询关于查询