麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

.Net 異步處理溫習(xí)

2019-11-17 03:52:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這幾天,看WF本質(zhì)論,里面提到了.net的異步處理。由于里面使用的是代碼片段,所以有點(diǎn)看不懂。于是下定決心,溫習(xí)一下.net中的異步處理。

使用C#在.net開(kāi)發(fā)已經(jīng)有5年了,最初使用.net中的異步處理大約是在4年前。當(dāng)時(shí),只是為了實(shí)現(xiàn)要求的功能,沒(méi)有詳細(xì)研究。這也難怪看WF時(shí)會(huì)頭暈(基礎(chǔ)不牢的后果呀)。

首先,我們分析一下異步處理的環(huán)境

需要在當(dāng)前線程中獲取返回值
不需要在當(dāng)前線程中獲取返回值,但是仍然需要對(duì)返回值做處理
對(duì)于第1中情況,還可以繼續(xù)細(xì)分

在當(dāng)前線程中啟動(dòng)線程T,然后繼續(xù)執(zhí)行當(dāng)前線程中的其它任務(wù),最后在當(dāng)前線程中獲取T的返回值
在當(dāng)前線程中啟動(dòng)線程T,然后繼續(xù)執(zhí)行當(dāng)前線程中的其它任務(wù)R1,等待T執(zhí)行完成,當(dāng)T執(zhí)行完成后,繼續(xù)執(zhí)行當(dāng)前線程中的其它任務(wù)R2,最后獲取T的返回值
在當(dāng)前線程中啟動(dòng)線程T,只要T在執(zhí)行就執(zhí)行任務(wù)R,最后獲取T的返回值
下面,我將一一給出例子:

1.1 在當(dāng)前線程中啟動(dòng)線程T,然后繼續(xù)執(zhí)行當(dāng)前線程中的其它任務(wù),最后在當(dāng)前線程中獲取T的返回值
view sourcePRint?01 using System;  

02 using System.Collections.Generic;  

03 using System.Linq;  

04 using System.Windows.Forms;  

05 using System.Threading;  

06 using System.Runtime.Remoting.Messaging;  

07 namespace FirstWF  

08 {  

09     static class Program  

10     {  

11         /// <summary>  

12         /// The main entry point for the application.  

13         /// </summary>  

14         [STAThread]  

15         static void Main()  

16         {  

17             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);  

18             Console.WriteLine("Input number please...");  

19             IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), null, null);  

20             Console.WriteLine("Implement other tasks");  

21             Thread.Sleep(7000);  

22             Console.WriteLine("Implement other tasks end ...");  

23             Console.WriteLine("Get user's input");  

24             Console.WriteLine(caller.EndInvoke(result));  

25             Console.ReadLine();  

26         }  

27         delegate string AsyncFuncDelegate(int userInput);  

28         static string Func(int userInput)  

29         {  

30             Console.WriteLine("Func start to run");  

31             Console.WriteLine("...");  

32             Thread.Sleep(5000);  

33             Console.WriteLine("Func end to run");  

34             return userInput.ToString();  

35         }  

36     }  

37 }

輸出結(jié)果如下:

Implement other tasks

Func start to run

...

Func end to run

Implement other tasks end ...

Get user's input

56

1.2 在當(dāng)前線程中啟動(dòng)線程T,然后繼續(xù)執(zhí)行當(dāng)前線程中的其它任務(wù)R1,等待T執(zhí)行完成,當(dāng)T執(zhí)行完成后,繼續(xù)執(zhí)行當(dāng)前線程中的其它任務(wù)R2,最后獲取T的返回值
view sourceprint?01 static void Main()  

02         {  

03             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);  

04             Console.WriteLine("Input number please...");  

05             IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), null, null);  

06             Console.WriteLine("Implement task 1");  

07             result.AsyncWaitHandle.WaitOne();  

08             result.AsyncWaitHandle.Close();  

09             Console.WriteLine("Implment task 2");  

10             Console.WriteLine("Get user's input");  

11             Console.WriteLine(caller.EndInvoke(result));  

12             Console.ReadLine();  

13         }

輸出結(jié)果如下:

Input number please...

25

Implement task 1

Func start to run

...

Func end to run

Implment task 2

Get user's input

25



1.3 在當(dāng)前線程中啟動(dòng)線程T,只要T在執(zhí)行就執(zhí)行任務(wù)R,最后獲取T的返回值
view sourceprint?01 [STAThread]  

02         static void Main()  

03         {  

04             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);  

05             Console.WriteLine("Input number please...");  

06             IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), null, null);  

07             while (!result.IsCompleted)  

08             {  

09                 Thread.Sleep(1000);  

10                 Console.Write(">");  

11             }  

12             Console.WriteLine("");  

13             Console.WriteLine("Implement other task2");  

14             Console.WriteLine("Get user's input");  

15             Console.WriteLine(caller.EndInvoke(result));  

16             Console.ReadLine();  

17         }

輸出結(jié)果如下:

Func start to run

...

>>>>>Func end to run

>

Implement other task2

Get user's input

23



2 不需要在當(dāng)前線程中獲取返回值,但是仍然需要對(duì)返回值做處理


view sourceprint?01 using System;  

02 using System.Collections.Generic;  

03 using System.Linq;  

04 using System.Windows.Forms;  

05 using System.Threading;  

06 using System.Runtime.Remoting.Messaging;  

07 namespace FirstWF  

08 {  

09     static class Program  

10     {  

11         /// <summary>  

12         /// The main entry point for the application.  

13         /// </summary>  

14         [STAThread]  

15         static void Main()  

16         {  

17             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);  

18             Console.WriteLine("Input number please...");  

19             caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), new AsyncCallback(CallBackFunc), "Message from Main thread.");  

20             Console.WriteLine("Main thread ends");  

21             Console.ReadLine();  

22         }  

23         delegate string AsyncFuncDelegate(int userInput);  

24         static string Func(int userInput)  

25         {  

26             Console.WriteLine("Func start to run");  

27             Console.WriteLine("...");  

28             Thread.Sleep(5000);  

29             Console.WriteLine("Func end to run");  

30             return userInput.ToString();  

31         }  

32         static void CallBackFunc(IAsyncResult ar)  

33         {  

34             AsyncResult result = ar as AsyncResult;  

35             string inputMessage = result.AsyncState as string;  

36             AsyncFuncDelegate caller = result.AsyncDelegate as AsyncFuncDelegate;  

37             Console.WriteLine("call back starts");  

38             Console.WriteLine(inputMessage);  

39             Console.WriteLine("The input number is : " + caller.EndInvoke(ar));  

40             Console.WriteLine("call back ends");  

41         }  

42     }  

43 }



輸出結(jié)果如下:

Input number please...

23

Main thread ends

Func start to run

...

Func end to run

call back starts

Message from Main thread.

The input number is : 23

call back ends



記得以前的代碼,寫(xiě)的都不是很好。雖然call.BeginInvoke可以開(kāi)始異步調(diào)用,但幾乎就沒(méi)有使用過(guò)EndInvoke。EndInvoke可以保證異步調(diào)用被正常結(jié)束,使代碼更加健康。

異步調(diào)用,可以使代碼具有更高的執(zhí)行效率,但是在異步調(diào)用時(shí),應(yīng)該有一個(gè)健康的使用習(xí)慣。


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: av中文字幕免费在线观看 | 羞羞视频免费网站 | 国产一级午夜 | 久久久久国产成人精品亚洲午夜 | 日韩午夜一区二区三区 | 深夜小视频在线观看 | 久久男人 | 国产免费一级淫片a级中文 99国产精品自拍 | 久久影院国产精品 | 中国大陆一级毛片 | 午夜久久视频 | 亚洲自拍第一 | 精品一二三区视频 | 日本在线不卡一区二区 | 免费观看国产精品视频 | 亚洲男人的天堂在线视频 | 国产毛片视频 | 国产午夜免费视频 | 思思久而久而蕉人 | 91久久国产露脸精品国产 | 黑人一区二区三区四区五区 | 亚洲一区二区三区精品在线观看 | 99re色| 一区二区免费 | 久久影院在线观看 | 久久精品成人免费国产片桃视频 | 黄色一级片毛片 | 日本在线播放一区二区 | 欧美黄色免费视频 | 亚洲欧美不卡视频 | 7777久久香蕉成人影院 | 九九综合九九 | 水多视频在线观看 | 情侣啪啪网站 | 欧美成人黄色小视频 | 欧美性色黄大片www 操碰网 | 成人男女啪啪免费观看网站四虎 | 精品1| 日韩视频在线观看免费 | 免费a视频在线观看 | 久久精品久久精品久久精品 |