瞭解異步起初,尤其是與回調是很難的。在您的例子中,你做出不正確的,但自然的假設......
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
}
然後通過另一種方法處理所有結果:
(!+1,彷彿他需要它大聲笑)
將在他的外觀極好的回答中指出,而不是有一個單獨的回調方法,你可以使用匿名方法與像lambda表達式:
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
}
因此,異步的過所有的心態是:
- 你的程序設置,並開始撥打服務電話,然後不斷做就是了任何其他人,無需等待。提示:這是一個自然開始加載動畫的地方,和/或禁用某些控件。
- 當你進行異步調用時,作爲參數之一,你給了一個方法在調用完成後運行。
- 當服務返回結果時,您指定的方法將運行以處理結果。提示:在這種方法中,您可以結束加載動畫,和/或重新啓用控件,並將結果插入到ViewModel中。
爲什麼不使用[任務](http://msdn.microsoft.com/en-us/library/dd537609.aspx)代替?他們通常更容易處理。 –
加入.net-4.0標籤 – EtherDragon