Configuration Managerでは、マネージド SMS プロバイダーを使用して非同期クエリを実行するには、ProcessQuery メソッドを使用します。
ProcessQuery メソッドの最初のパラメーターは、2 つのイベント ハンドラーを提供する SmsBackgroundWorker クラスのインスタンスです。
QueryProcessObjectReady。 このイベント ハンドラーは、クエリによって返されるオブジェクトごとに呼び出されます。 イベント ハンドラーは、 オブジェクトを表す IResultObject オブジェクトを提供します。
QueryProcessCompleted。 このイベント ハンドラーは、クエリが完了したときに呼び出されます。 また、発生したエラーに関する情報も提供します。 詳細については、「マネージド コードを使用してConfiguration Manager非同期エラーを処理する方法」を参照してください。
ProcessQuery メソッドの 2 番目のパラメーターは、クエリの 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拡張 WMI クエリ言語を使用して同期Configuration Manager クエリを実行する方法Configuration Manager結果セットクエリに関する特殊なクエリのConfiguration Manager