0
A
回答
4
在C#上(不僅在WP7上),您可以通過將其包裝在委託中來異步調用任何函數。在委託的BeginInvoke調用中,您將傳遞一個回調,該操作完成後將調用該回調。看下面的例子:
int MyLongOperation(int x, int y) {
Thread.Sleep(10000);
return x+y;
}
void CallingLongOperation(){
int x = 4;
int y = 5;
Func<int, int, int> func = MyLongOperation;
func.BeginInvoke(x, y, OperationCallback, func);
}
void OperationCallback(IAsyncResult asyncResult) {
Func<int, int, int> func = (Func<int, int, int>) asyncResult.AsyncState;
int result = func.EndInvoke(asyncResult);
// do something with the result
}
如果你需要傳遞一些附加參數在asyncState/userState屬性,您還可以使用的IAsyncResult參數的AsyncDelegate特性(這對於委託調用總是System.Runtime.Remoting .Messaging.AsyncResult),並從那裏檢索委託,如下所示。
public int MyLongOperation(int x, int y)
{
Thread.Sleep(10000);
return x + y;
}
public void CallLongOperation()
{
Func<int, int, int> func = MyLongOperation;
func.BeginInvoke(5, 7, MyCallback, "Expected result: " + 12);
Console.WriteLine("Called BeginInvoke");
func.BeginInvoke(11, 22, MyCallback, "Expected result: " + 33);
Console.WriteLine("Press ENTER to continue");
Console.ReadLine();
}
void MyCallback(IAsyncResult asyncResult)
{
Func<int, int, int> func = (Func<int, int, int>)((System.Runtime.Remoting.Messaging.AsyncResult)asyncResult).AsyncDelegate;
string expectedResult = (string)asyncResult.AsyncState;
int result = func.EndInvoke(asyncResult);
Console.WriteLine("Result: {0} - {1}", result, expectedResult);
}
相關問題
- 1. 在WP7中實現回滾事務
- 2. 如何在C++中實現回調?
- 3. 在opa中實現回調
- 4. 在Python和C之間實現回調
- 5. C++ -fpermissive錯誤,同時在C++中實現回調
- 6. wp7中的增強現實
- 7. 停止時鐘在wp7中的實現
- 8. 在WP7中實現數據庫芒果
- 9. Cython - 實現回調
- 10. 在ASP.NET中實現JavaScript回調?
- 11. 如何在Ruby中實現「回調」?
- 12. 無法在Android中實現回調
- 13. 在時間軸中實現回調
- 14. 如何在JavaScript中實現before_filter回調?
- 15. 在Delphi中實現DLL回調
- 16. 在XML-RPC或SOAP中實現回調
- 17. 如何在Angular4中實現ngFor回調?
- 18. 如何在Java中實現回調
- 19. 在C++中實現可用回調的好方法
- 20. 我可以在C++中實現多個回調接口嗎?
- 21. 來自WCF的WP7回調
- 22. 如何在Silverlight/WP7中實現實時圖形?
- 23. 包裝本機庫,實現在C#回調#
- 24. 如何實現一個回調URL接收JSON數據在C#
- 25. Pubsubhubbub訂閱PHP中的回調實現
- 26. WP7從BeginGetResponse回調中傳播異常
- 27. 調度算法實現C++
- 28. 在WP7中調用Webservices
- 29. 稀疏束調整實現c/C++
- 30. 在C#中實現DataCash 3DSecure
委託不能在.NET Compact Framework上因此異步調用,因此WP7。 – 2012-04-27 04:00:08