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

C#異步編程模式IAsyncResult淺析

開發(fā) 后端
C#異步編程模式IAsyncResult是什么呢?我們需要明白C#異步編程模式IAsyncResult什么方面呢?那么本文就向你介紹相關(guān)的內(nèi)容。

C#異步編程模式IAsyncResult概述

IAsyncResult 異步設(shè)計(jì)模式通過名為 BeginOperationName 和 EndOperationName 的兩個(gè)方法來實(shí)現(xiàn)原同步方法的異步調(diào)用,如 FileStream 類提供了 BeginRead 和 EndRead 方法來從文件異步讀取字節(jié),它們是 Read 方法的異步版本

Begin 方法包含同步方法簽名中的任何參數(shù),此外還包含另外兩個(gè)參數(shù):一個(gè)AsyncCallback 委托和一個(gè)用戶定義的狀態(tài)對(duì)象。委托用來調(diào)用回調(diào)方法,狀態(tài)對(duì)象是用來向回調(diào)方法傳遞狀態(tài)信息。該方法返回一個(gè)實(shí)現(xiàn) IAsyncResult 接口的對(duì)象

End 方法用于結(jié)束異步操作并返回結(jié)果,因此包含同步方法簽名中的 ref 和 out 參數(shù),返回值類型也與同步方法相同。該方法還包括一個(gè) IAsyncResult 參數(shù),用于獲取異步操作是否完成的信息,當(dāng)然在使用時(shí)就必須傳入對(duì)應(yīng)的 Begin 方法返回的對(duì)象實(shí)例

開始異步操作后如果要阻止應(yīng)用程序,可以直接調(diào)用 End 方法,這會(huì)阻止應(yīng)用程序直到異步操作完成后再繼續(xù)執(zhí)行。也可以使用 IAsyncResult 的 AsyncWaitHandle 屬性,調(diào)用其中的WaitOne等方法來阻塞線程。這兩種方法的區(qū)別不大,只是前者必須一直等待而后者可以設(shè)置等待超時(shí)

如果不阻止應(yīng)用程序,則可以通過輪循 IAsyncResult 的 IsCompleted 狀態(tài)來判斷操作是否完成,或使用 AsyncCallback 委托來結(jié)束異步操作。AsyncCallback 委托包含一個(gè) IAsyncResult 的簽名,回調(diào)方法內(nèi)部再調(diào)用 End 方法來獲取操作執(zhí)行結(jié)果

C#異步編程模式IAsyncResult之IAsyncResult 接口

  1. public interface IAsyncResult  
  2. {  
  3. object AsyncState { get; }  
  4. WaitHandle AsyncWaitHandle { get; }  
  5. bool CompletedSynchronously { get; }  
  6. bool IsCompleted { get; }  

我用一個(gè) AsyncDemo 類作為異步方法的提供者,后面的程序都會(huì)調(diào)用它。內(nèi)部很簡單,構(gòu)造函數(shù)接收一個(gè)字符串作為 name ,Run 方法輸出 "My name is " + name ,而異步方法直接用委托的 BeginInvoke 和 EndInvoke 方法實(shí)現(xiàn)

  1. public class AsyncDemo  
  2. {  
  3. // Use in asynchronous methods  
  4. private delegate string runDelegate();  
  5.  
  6. private string m_Name;  
  7. private runDelegate m_Delegate;  
  8.  
  9. public AsyncDemo(string name)  
  10. {  
  11. m_Name = name;  
  12. m_Delegate = new runDelegate(Run);  
  13. }  
  14.  
  15. /**//// ﹤summary﹥  
  16. /// Synchronous method  
  17. /// ﹤/summary﹥  
  18. /// ﹤returns﹥﹤/returns﹥  
  19. public string Run()  
  20. {  
  21. return "My name is " + m_Name;  
  22. }  
  23.  
  24. /**//// ﹤summary﹥  
  25. /// Asynchronous begin method  
  26. /// ﹤/summary﹥  
  27. /// ﹤param name="callBack"﹥﹤/param﹥  
  28. /// ﹤param name="stateObject"﹥﹤/param﹥  
  29. /// ﹤returns﹥﹤/returns﹥  
  30. public IAsyncResult BeginRun(  
  31. AsyncCallback callBack, Object stateObject)  
  32. {  
  33. try 
  34. {  
  35. return m_Delegate.BeginInvoke(callBack, stateObject);  
  36. }  
  37. catch(Exception e)  
  38. {  
  39. // Hide inside method invoking stack  
  40. throw e;  
  41. }  
  42. }  
  43.  
  44. /**//// ﹤summary﹥  
  45. /// Asynchronous end method  
  46. /// ﹤/summary﹥  
  47. /// ﹤param name="ar"﹥﹤/param﹥  
  48. /// ﹤returns﹥﹤/returns﹥  
  49. public string EndRun(IAsyncResult ar)  
  50. {  
  51. if (ar == null)  
  52. throw new NullReferenceException(  
  53. "Arggument ar can't be null");  
  54.  
  55. try 
  56. {  
  57. return m_Delegate.EndInvoke(ar);  
  58. }  
  59. catch (Exception e)  
  60. {  
  61. // Hide inside method invoking stack  
  62. throw e;  
  63. }  
  64. }  

C#異步編程模式IAsyncResult操作步驟:首先是 Begin 之后直接調(diào)用 End 方法,當(dāng)然中間也可以做其他的操作

  1. class AsyncTest  
  2. {  
  3. static void Main(string[] args)  
  4. {  
  5. AsyncDemo demo = new AsyncDemo("jiangnii");  
  6.  
  7. // Execute begin method  
  8. IAsyncResult ar = demo.BeginRun(nullnull);  
  9.  
  10. // You can do other things here  
  11.  
  12. // Use end method to block thread  
  13. // until the operation is complete  
  14. string demoName = demo.EndRun(ar);  
  15. Console.WriteLine(demoName);  
  16. }  

也可以用 IAsyncResult 的 AsyncWaitHandle 屬性,我在這里設(shè)置為1秒超時(shí)

  1. class AsyncTest  
  2. {  
  3. static void Main(string[] args)  
  4. {  
  5. AsyncDemo demo = new AsyncDemo("jiangnii");  
  6.  
  7. // Execute begin method  
  8. IAsyncResult ar = demo.BeginRun(nullnull);  
  9.  
  10. // You can do other things here  
  11.  
  12. // Use AsyncWaitHandle.WaitOne method to block thread for 1 second at most  
  13. ar.AsyncWaitHandle.WaitOne(1000, false);  
  14.  
  15. if (ar.IsCompleted)  
  16. {  
  17. // Still need use end method to get result,  
  18. // but this time it will return immediately   
  19. string demoName = demo.EndRun(ar);  
  20. Console.WriteLine(demoName);  
  21. }  
  22. else 
  23. {  
  24. Console.WriteLine("Sorry,  
  25. can't get demoName, the time is over");  
  26. }  
  27. }  

C#異步編程模式IAsyncResult要注意的還有:不中斷的循環(huán),每次循環(huán)輸出一個(gè) "."

  1. class AsyncTest  
  2. {  
  3. static void Main(string[] args)  
  4. {  
  5. AsyncDemo demo = new AsyncDemo("jiangnii");  
  6.  
  7. // Execute begin method  
  8. IAsyncResult ar = demo.BeginRun(nullnull);  
  9.  
  10. Console.Write("Waiting..");  
  11. while (!ar.IsCompleted)  
  12. {  
  13. Console.Write(".");  
  14. // You can do other things here  
  15. }  
  16. Console.WriteLine();  
  17.  
  18. // Still need use end method to get result,   
  19. //but this time it will return immediately   
  20. string demoName = demo.EndRun(ar);  
  21. Console.WriteLine(demoName);  
  22. }  
  23. }  

***是使用回調(diào)方法并加上狀態(tài)對(duì)象,狀態(tài)對(duì)象被作為 IAsyncResult 參數(shù)的 AsyncState 屬性被傳給回調(diào)方法?;卣{(diào)方法執(zhí)行前不能讓主線程退出,我這里只是簡單的讓其休眠了1秒。另一個(gè)與之前不同的地方是 AsyncDemo 對(duì)象被定義成了類的靜態(tài)字段,以便回調(diào)方法使用

  1. class AsyncTest  
  2. {  
  3. static AsyncDemo demo = new AsyncDemo("jiangnii");  
  4.  
  5. static void Main(string[] args)  
  6. {  
  7. // State object  
  8. bool state = false;  
  9.  
  10. // Execute begin method  
  11. IAsyncResult ar = demo.BeginRun(  
  12. new AsyncCallback(outPut), state);  
  13.  
  14. // You can do other thins here  
  15.  
  16. // Wait until callback finished  
  17. System.Threading.Thread.Sleep(1000);  
  18. }  
  19.  
  20. // Callback method  
  21. static void outPut(IAsyncResult ar)  
  22. {  
  23. bool state = (bool)ar.AsyncState;  
  24. string demoName = demo.EndRun(ar);  
  25.  
  26. if (state)  
  27. {  
  28. Console.WriteLine(demoName);  
  29. }  
  30. else 
  31. {  
  32. Console.WriteLine(demoName + ", isn't it?");  
  33. }  
  34. }  

C#異步編程模式IAsyncResult的后話:

對(duì)于一個(gè)已經(jīng)實(shí)現(xiàn)了 BeginOperationName 和 EndOperationName方法的對(duì)象,我們可以直接用上述方式調(diào)用,但對(duì)于只有同步方法的對(duì)象,我們要對(duì)其進(jìn)行異步調(diào)用也不需要增加對(duì)應(yīng)的異步方法,而只需定義一個(gè)委托并使用其 BeginInvoke 和 EndInvoke 方法就可以了。

C#異步編程模式IAsyncResult的基本情況就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#異步編程模式IAsyncResult有所幫助。

【編輯推薦】

  1. C#改寫方法學(xué)習(xí)筆記
  2. 概述C#加框和消框
  • 淺析C#異步操作
  • 描述C#異步Socket
  • C# Socket異步通訊實(shí)現(xiàn)詳解
  • 責(zé)任編輯:仲衡 來源: 博客園
    相關(guān)推薦

    2009-08-20 17:47:54

    C#異步編程模式

    2009-08-17 13:34:02

    C#異步操作

    2009-08-21 10:17:14

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

    2009-08-21 09:20:44

    C#異步套接字

    2009-08-27 14:12:02

    C# interfac

    2021-10-12 17:47:22

    C# TAP異步

    2015-09-16 15:11:58

    C#異步編程

    2009-08-20 18:47:19

    C#異步通信

    2009-08-21 11:24:16

    C#異步調(diào)用

    2009-09-07 04:56:52

    C#模式窗體

    2009-08-26 09:48:48

    C#異步套接字

    2009-08-21 14:03:04

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

    2009-03-10 13:59:41

    C#套接字編程

    2009-09-07 09:53:01

    C# DisposeDispose方法

    2009-01-16 09:58:07

    C#編程C#內(nèi)存管理垃圾收集

    2009-08-31 17:02:28

    C#接口編程

    2009-04-29 09:06:18

    C#設(shè)計(jì)模式Adapter

    2009-08-21 11:31:59

    異步和多線程的區(qū)別

    2009-08-17 14:36:15

    C#進(jìn)度條實(shí)現(xiàn)

    2009-08-20 18:37:52

    委托C#異步委托
    點(diǎn)贊
    收藏

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