自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#異步調(diào)用四大方法詳解

開(kāi)發(fā) 后端
C#異步調(diào)用四大方法是什么呢?他們各自的作用是什么呢?那么本文就向你介紹C#異步調(diào)用四大方法的具體內(nèi)容。

C#異步調(diào)用四大方法是什么呢?C#異步調(diào)用四大方法的使用是如何進(jìn)行的呢?讓我們首先了解下什么時(shí)候用到C#異步調(diào)用:

.NET Framework 允許您C#異步調(diào)用任何方法。定義與您需要調(diào)用的方法具有相同簽名的委托;公共語(yǔ)言運(yùn)行庫(kù)將自動(dòng)為該委托定義具有適當(dāng)簽名的 BeginInvoke 和 EndInvoke 方法。

BeginInvoke 方法用于啟動(dòng)C#異步調(diào)用。它與您需要異步執(zhí)行的方法具有相同的參數(shù),只不過(guò)還有兩個(gè)額外的參數(shù)(將在稍后描述)。BeginInvoke 立即返回,不等待C#異步調(diào)用完成。BeginInvoke 返回 IasyncResult,可用于監(jiān)視調(diào)用進(jìn)度。

EndInvoke 方法用于檢索C#異步調(diào)用結(jié)果。調(diào)用 BeginInvoke 后可隨時(shí)調(diào)用 EndInvoke 方法;如果C#異步調(diào)用未完成,EndInvoke 將一直阻塞到C#異步調(diào)用完成。EndInvoke 的參數(shù)包括您需要異步執(zhí)行的方法的 out 和 ref 參數(shù)(在 Visual Basic 中為 ByRef 和 ByRef)以及由 BeginInvoke 返回的 IAsyncResult。

注意   Visual Studio .NET 中的智能感知功能會(huì)顯示 BeginInvoke 和 EndInvoke 的參數(shù)。如果您沒(méi)有使用 Visual Studio 或類似的工具,或者您使用的是 C# 和 Visual Studio .NET,請(qǐng)參見(jiàn)異步方法簽名獲取有關(guān)運(yùn)行庫(kù)為這些方法定義的參數(shù)的描述。

本主題中的代碼演示了四種使用 BeginInvoke 和 EndInvoke 進(jìn)行C#異步調(diào)用的常用方法。調(diào)用了 BeginInvoke 后,可以:

· 進(jìn)行某些操作,然后調(diào)用 EndInvoke 一直阻塞到調(diào)用完成。

· 使用 IAsyncResult.AsyncWaitHandle 獲取 WaitHandle,使用它的 WaitOne 方法將執(zhí)行一直阻塞到發(fā)出 WaitHandle 信號(hào),然后調(diào)用 EndInvoke。

· 輪詢由 BeginInvoke 返回的 IAsyncResult,確定C#異步調(diào)用何時(shí)完成,然后調(diào)用 EndInvoke。

· 將用于回調(diào)方法的委托傳遞給 BeginInvoke。該方法在C#異步調(diào)用完成后在 ThreadPool 線程上執(zhí)行,它可以調(diào)用 EndInvoke。

警告:始終在C#異步調(diào)用完成后調(diào)用 EndInvoke。

測(cè)試方法和異步委托

四個(gè)示例全部使用同一個(gè)長(zhǎng)期運(yùn)行的測(cè)試方法 TestMethod。該方法顯示一個(gè)表明它已開(kāi)始處理的控制臺(tái)信息,休眠幾秒鐘,然后結(jié)束。TestMethod 有一個(gè) out 參數(shù)(在 Visual Basic 中為 ByRef),它演示了如何將這些參數(shù)添加到 BeginInvoke 和 EndInvoke 的簽名中。您可以用類似的方式處理 ref 參數(shù)(在 Visual Basic 中為 ByRef)。

下面的代碼示例顯示 TestMethod 以及代表 TestMethod 的委托;若要使用任一示例,請(qǐng)將示例代碼追加到這段代碼中。

注意   為了簡(jiǎn)化這些示例,TestMethod 在獨(dú)立于 Main() 的類中聲明?;蛘撸琓estMethod 可以是包含 Main() 的同一類中的 static 方法(在 Visual Basic 中為 Shared)。

  1. using System;  
  2. using System.Threading;   
  3.  
  4. public class AsyncDemo {  
  5. // The method to be executed asynchronously.  
  6. //  
  7. public string TestMethod(  
  8. int callDuration, out int threadId) {  
  9. Console.WriteLine("Test method begins.");  
  10. Thread.Sleep(callDuration);  
  11. threadId = AppDomain.GetCurrentThreadId();  
  12. return "MyCallTime was " + callDuration.ToString();  
  13. }  
  14. }  
  15.  
  16. // The delegate must have the same signature as the method  
  17. // you want to call asynchronously.  
  18. public delegate string AsyncDelegate(  
  19. int callDuration, out int threadId);  
  20.    
  21.  
  22. using System;  
  23. using System.Threading;   
  24.  
  25. public class AsyncDemo {  
  26. // The method to be executed asynchronously.  
  27. //  
  28. public string TestMethod(  
  29. int callDuration, out int threadId) {  
  30. Console.WriteLine("Test method begins.");  
  31. Thread.Sleep(callDuration);  
  32. threadId = AppDomain.GetCurrentThreadId();  
  33. return "MyCallTime was " + callDuration.ToString();  
  34. }  
  35. }  
  36.  
  37. // The delegate must have the same signature as the method  
  38. // you want to call asynchronously.  
  39. public delegate string AsyncDelegate(  
  40. int callDuration, out int threadId); 

C#異步調(diào)用四大方法之使用 EndInvoke 等待異步調(diào)用

異步執(zhí)行方法的最簡(jiǎn)單方式是以 BeginInvoke 開(kāi)始,對(duì)主線程執(zhí)行一些操作,然后調(diào)用 EndInvoke。EndInvoke 直到C#異步調(diào)用完成后才返回。這種技術(shù)非常適合文件或網(wǎng)絡(luò)操作,但是由于它阻塞 EndInvoke,所以不要從用戶界面的服務(wù)線程中使用它。

  1. public class AsyncMain {  
  2. static void Main(string[] args) {  
  3. // The asynchronous method puts the thread id here.  
  4. int threadId;  
  5.  
  6. // Create an instance of the test class.  
  7. AsyncDemo ad = new AsyncDemo();  
  8.  
  9. // Create the delegate.  
  10. AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);  
  11.      
  12. // Initiate the asychronous call.  
  13. IAsyncResult ar = dlgt.BeginInvoke(3000,   
  14. out threadId, nullnull);  
  15.  
  16. Thread.Sleep(0);  
  17. Console.WriteLine("Main thread {0} does some work.",  
  18. AppDomain.GetCurrentThreadId());  
  19.  
  20. // Call EndInvoke to Wait for   
  21. //the asynchronous call to complete,  
  22. // and to retrieve the results.  
  23. string ret = dlgt.EndInvoke(out threadId, ar);  
  24.  
  25. Console.WriteLine("The call executed on thread {0},   
  26. with return value \"{1}\".", threadId, ret);  
  27. }  

C#異步調(diào)用四大方法之使用 WaitHandle 等待異步調(diào)用

等待 WaitHandle 是一項(xiàng)常用的線程同步技術(shù)。您可以使用由 BeginInvoke 返回的 IAsyncResult 的 AsyncWaitHandle 屬性來(lái)獲取 WaitHandle。C#異步調(diào)用完成時(shí)會(huì)發(fā)出 WaitHandle 信號(hào),而您可以通過(guò)調(diào)用它的 WaitOne 等待它。

如果您使用 WaitHandle,則在C#異步調(diào)用完成之后,但在通過(guò)調(diào)用 EndInvoke 檢索結(jié)果之前,可以執(zhí)行其他處理。

  1. public class AsyncMain {  
  2. static void Main(string[] args) {  
  3. // The asynchronous method puts the thread id here.  
  4. int threadId;  
  5.  
  6. // Create an instance of the test class.  
  7. AsyncDemo ad = new AsyncDemo();  
  8.  
  9. // Create the delegate.  
  10. AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);  
  11.      
  12. // Initiate the asychronous call.  
  13. IAsyncResult ar = dlgt.BeginInvoke(3000,   
  14. out threadId, nullnull);  
  15.  
  16. Thread.Sleep(0);  
  17. Console.WriteLine("Main thread {0} does some work.",  
  18. AppDomain.GetCurrentThreadId());  
  19.  
  20. // Wait for the WaitHandle to become signaled.  
  21. ar.AsyncWaitHandle.WaitOne();  
  22.  
  23. // Perform additional processing here.  
  24. // Call EndInvoke to retrieve the results.  
  25. string ret = dlgt.EndInvoke(out threadId, ar);  
  26.  
  27. Console.WriteLine("The call executed on thread {0},   
  28. with return value \"{1}\".", threadId, ret);  
  29. }  

C#異步調(diào)用四大方法之輪詢異步調(diào)用完成

您可以使用由 BeginInvoke 返回的 IAsyncResult 的 IsCompleted 屬性來(lái)發(fā)現(xiàn)C#異步調(diào)用何時(shí)完成。從用戶界面的服務(wù)線程中進(jìn)行C#異步調(diào)用時(shí)可以執(zhí)行此操作。輪詢完成允許用戶界面線程繼續(xù)處理用戶輸入。

  1. public class AsyncMain {  
  2. static void Main(string[] args) {  
  3. // The asynchronous method puts the thread id here.  
  4. int threadId;  
  5.  
  6. // Create an instance of the test class.  
  7. AsyncDemo ad = new AsyncDemo();  
  8.  
  9. // Create the delegate.  
  10. AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);  
  11.      
  12. // Initiate the asychronous call.  
  13. IAsyncResult ar = dlgt.BeginInvoke(3000,   
  14. out threadId, nullnull);  
  15.  
  16. // Poll while simulating work.  
  17. while(ar.IsCompleted == false) {  
  18. Thread.Sleep(10);  
  19. }  
  20.  
  21. // Call EndInvoke to retrieve the results.  
  22. string ret = dlgt.EndInvoke(out threadId, ar);  
  23.  
  24. Console.WriteLine("The call executed on thread {0},  
  25.  with return value \"{1}\".", threadId, ret);  
  26. }  

C#異步調(diào)用四大方法之異步調(diào)用完成時(shí)執(zhí)行回調(diào)方法

如果啟動(dòng)異步調(diào)用的線程不需要處理調(diào)用結(jié)果,則可以在調(diào)用完成時(shí)執(zhí)行回調(diào)方法。回調(diào)方法在 ThreadPool 線程上執(zhí)行。

要使用回調(diào)方法,必須將代表該方法的 AsyncCallback 委托傳遞給 BeginInvoke。也可以傳遞包含回調(diào)方法將要使用的信息的對(duì)象。例如,可以傳遞啟動(dòng)調(diào)用時(shí)曾使用的委托,以便回調(diào)方法能夠調(diào)用 EndInvoke。

  1. public class AsyncMain {  
  2. // Asynchronous method puts the thread id here.  
  3. private static int threadId;  
  4.  
  5. static void Main(string[] args) {  
  6. // Create an instance of the test class.  
  7. AsyncDemo ad = new AsyncDemo();  
  8.  
  9. // Create the delegate.  
  10. AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);  
  11.      
  12. // Initiate the asychronous call.  Include an AsyncCallback  
  13. // delegate representing the callback method, and the data  
  14. // needed to call EndInvoke.  
  15. IAsyncResult ar = dlgt.BeginInvoke(3000,  
  16. out threadId,   
  17. new AsyncCallback(CallbackMethod),  
  18. dlgt );  
  19.  
  20. Console.WriteLine("Press Enter to close application.");  
  21. Console.ReadLine();  
  22. }  
  23.  
  24. // Callback method must have the same signature as the  
  25. // AsyncCallback delegate.  
  26. static void CallbackMethod(IAsyncResult ar) {  
  27. // Retrieve the delegate.  
  28. AsyncDelegate dlgt = (AsyncDelegate) ar.AsyncState;  
  29.  
  30. // Call EndInvoke to retrieve the results.  
  31. string ret = dlgt.EndInvoke(out threadId, ar);  
  32.  
  33. Console.WriteLine("The call executed on thread {0},  
  34.  with return value \"{1}\".", threadId, ret);  
  35. }  
  36. }  

C#異步調(diào)用四大方法的基本內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#異步調(diào)用有所幫助。

【編輯推薦】

  1. FlyTcpFramework在C#異步中的應(yīng)用
  2. C#異步調(diào)用的應(yīng)用實(shí)踐淺談
  3. 委托實(shí)現(xiàn)C#異步調(diào)用淺析
  4. 淺析C#中異步和多線程的區(qū)別
  5. C# Socket通信三大問(wèn)題詳解
責(zé)任編輯:仲衡 來(lái)源: 博客園
相關(guān)推薦

2009-10-20 16:48:30

C#委托

2009-08-21 11:24:16

C#異步調(diào)用

2009-08-21 11:02:55

C#異步調(diào)用

2009-08-20 19:08:30

C#異步調(diào)用

2009-08-28 11:43:26

C#數(shù)組初始化

2009-08-21 10:17:14

C#異步網(wǎng)絡(luò)編程

2021-08-13 09:55:42

數(shù)據(jù)中心功耗IT支出

2021-07-30 17:47:46

數(shù)據(jù)中心電力能源

2009-11-06 15:54:15

WCF異步調(diào)用

2009-08-17 08:01:00

C#文件列表

2021-03-29 09:26:44

SpringBoot異步調(diào)用@Async

2010-01-11 17:24:19

VB.NET異步調(diào)用

2011-11-14 09:58:33

2021-10-21 08:49:36

物聯(lián)網(wǎng)市場(chǎng)物聯(lián)網(wǎng)IOT

2009-09-02 10:49:46

C#調(diào)用析構(gòu)方法

2009-08-17 16:49:46

C#多線程控制

2024-07-31 15:57:41

2024-10-15 10:28:43

2009-12-07 14:26:47

WCF異步調(diào)用

2009-09-01 09:37:15

C#寫(xiě)文件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)