特にコールバックを使用して、最初に非同期を理解するのは難しいです。あなたの例では、間違っていますが、自然な仮定をしています...
public static Object LoadInfo()
{
var service = new SomeWcfService();
service.BeginGetInfo(CallbackMethod, service);
// HOW TO GET INFROMATION FROM CALLBACK??
// ERROR: You assume you have more work to do in this method,
// or that this is the place to return your results.
return INFORMATION;
}
以下に示す方法は、結果が返された後に作業が行われる場所です。
private static void CallbackMethod(IAsyncResult ar)
{
// HOW TO PASS INFROMATION TO LoadInfo??
// OOPS! No need to pass pack to LoadInfo - it's done...
var INFORMATION = (ar.AsyncState as SomeWcfService).EndGetInfo(ar);
}
代わりに、このようなものが必要になります
public static void LoadInfo()
{
var service = new SomeWcfService();
// begin an asynchronous service call
// and handle the results in another method, "CallbackMethod"
service.BeginGetInfo(CallbackMethod, service);
// You can do other, non-service related,
// things here while the service call executes
}
次に、他のメソッドがすべての結果を処理します。
private static void CallbackMethod(IAsyncResult ar)
{
var results = (ar.AsyncState as SomeWcfService).EndGetInfo(ar);
// Do whetever you need with results here
}
Will が彼の優れた回答で指摘したように (+1、彼が必要としているかのように笑!)、別のコールバック メソッドを使用する代わりに、次のようなラムダ式で匿名メソッドを使用できます。
public static void LoadInfo()
{
var service = new SomeWcfService();
// begin an asynchronous service call
// and handle the results in this anonymous method
service.BeginGetInfo(x =>
{
// B. This code block will be called when the service returns with results
var results = (ar.AsyncState as SomeWcfService).EndGetInfo(ar);
// Do whetever you need with results here
}, service);
// A. You can do other things here while the service call executes
// but debugging this gets more complicated because things will likely
// occur at A before they occur at B
}
したがって、非同期の全体的な考え方は次のとおりです。
- あなたのプログラムは、サービス呼び出しをセットアップして開始し、待機することなく、必要なことを続けます。ヒント: これは、ローディング アニメーションを開始したり、一部のコントロールを無効にしたりするのに自然な場所です。
- 非同期呼び出しを行ったとき、パラメーターの 1 つとして、呼び出しが完了したときに実行するメソッドを指定しました。
- サービスが結果を返すと、指定したメソッドが実行され、結果に対する作業が行われます。ヒント: このメソッドでは、ロード アニメーションを終了したり、コントロールを再度有効にしたり、結果を ViewModel にプラグインしたりできます。