在 Configuration Manager 中,若要使用託管 SMS 提供者執行非同步查詢,請使用 ProcessQuery 方法。
ProcessQuery 方法的第一個參數是 SmsBackgroundWorker 類別的執行個體,提供兩個事件處理常式:
QueryProcessObjectReady。 系統會針對查詢所傳回的每個物件呼叫此事件處理常式。 事件處理常式提供代表物件的 IResultObject 物件。
QueryProcessCompleted。 此事件處理常式會在查詢完成時呼叫。 它還提供了關於發生的任何錯誤的資訊。 如需詳細資訊,請參閱 如需錯誤處理的詳細資訊,請參閱 如何使用 Managed 程式碼處理 Configuration Manager 非同步錯誤。
ProcessQuery 方法的第二個參數 to 是查詢的 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...");
}
此範例方法具有下列參數:
| 參數 | Type | 描述 |
|---|---|---|
connection |
受控: WqlConnectionManager |
與 SMS 提供者的有效連線。 |
正在編譯程式碼
命名空間
系統
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
組件
Microsoft.ConfigurationManagement.ManagementProvider
adminui.wqlqueryengine
健全的程式設計
可能引發的 Configuration Manager 例外狀況包括 SmsConnectionException 和 SmsQueryException。 這些可以與 SmsException 一起捕捉。
另請參閱
物件概觀:Configuration Manager 消極屬性如何使用 Managed 程式碼呼叫 Configuration Manager 物件類別方法如何使用 Managed 程式碼連線到 Configuration Manager 提供者如何建立使用 Managed 程式碼的 Configuration Manager 物件如何使用 Managed 程式碼修改 Configuration Manager 物件如何使用 Managed 程式碼執行同步 Configuration Manager 查詢如何讀取 Configuration Manager使用 Managed 程式碼建立物件如何使用 Managed 程式碼來讀取消極式屬性如何使用 Managed Configuration Manager 執行同步 Configuration Manager 查詢 延伸 WMI 查詢語言:Configuration Manager 結果集關於查詢的 Configuration Manager 特殊查詢