最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

.Net 异步处理温习技巧

日期:02-17    来源:中国设计秀    作者:cnwebshow.com

这几天,看WF本质论,里面提到了.net的异步处理。由于里面使用的是代码片段,所以有点看不懂。于是下定决心,温习一下.net中的异步处理。5hb中国设计秀
5hb中国设计秀
使用C#在.net开发已经有5年了,最初使用.net中的异步处理大约是在4年前。当时,只是为了实现要求的功能,没有详细研究。这也难怪看WF时会头晕(基础不牢的后果呀)。 5hb中国设计秀
5hb中国设计秀
首先,我们分析一下异步处理的环境 5hb中国设计秀
5hb中国设计秀
需要在当前线程中获取返回值 5hb中国设计秀
不需要在当前线程中获取返回值,但是仍然需要对返回值做处理5hb中国设计秀
对于第1中情况,还可以继续细分 5hb中国设计秀
5hb中国设计秀
在当前线程中启动线程T,然后继续执行当前线程中的其它任务,最后在当前线程中获取T的返回值 5hb中国设计秀
在当前线程中启动线程T,然后继续执行当前线程中的其它任务R1,等待T执行完成,当T执行完成后,继续执行当前线程中的其它任务R2,最后获取T的返回值 5hb中国设计秀
在当前线程中启动线程T,只要T在执行就执行任务R,最后获取T的返回值5hb中国设计秀
下面,我将一一给出例子:5hb中国设计秀
5hb中国设计秀
1.1 在当前线程中启动线程T,然后继续执行当前线程中的其它任务,最后在当前线程中获取T的返回值5hb中国设计秀
view sourcePRint?01 using System;  5hb中国设计秀
5hb中国设计秀
02 using System.Collections.Generic;  5hb中国设计秀
5hb中国设计秀
03 using System.Linq;  5hb中国设计秀
5hb中国设计秀
04 using System.Windows.Forms;  5hb中国设计秀
5hb中国设计秀
05 using System.Threading;  5hb中国设计秀
5hb中国设计秀
06 using System.Runtime.Remoting.Messaging;  5hb中国设计秀
5hb中国设计秀
07 namespace FirstWF  5hb中国设计秀
5hb中国设计秀
08 {  5hb中国设计秀
5hb中国设计秀
09     static class Program  5hb中国设计秀
5hb中国设计秀
10     {  5hb中国设计秀
5hb中国设计秀
11         /// <summary>  5hb中国设计秀
5hb中国设计秀
12         /// The main entry point for the application.  5hb中国设计秀
5hb中国设计秀
13         /// </summary>  5hb中国设计秀
5hb中国设计秀
14         [STAThread]  5hb中国设计秀
5hb中国设计秀
15         static void Main()  5hb中国设计秀
5hb中国设计秀
16         {  5hb中国设计秀
5hb中国设计秀
17             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);  5hb中国设计秀
5hb中国设计秀
18             Console.WriteLine("Input number please...");  5hb中国设计秀
5hb中国设计秀
19             IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), null, null);  5hb中国设计秀
5hb中国设计秀
20             Console.WriteLine("Implement other tasks");  5hb中国设计秀
5hb中国设计秀
21             Thread.Sleep(7000);  5hb中国设计秀
5hb中国设计秀
22             Console.WriteLine("Implement other tasks end ...");  5hb中国设计秀
5hb中国设计秀
23             Console.WriteLine("Get user's input");  5hb中国设计秀
5hb中国设计秀
24             Console.WriteLine(caller.EndInvoke(result));  5hb中国设计秀
5hb中国设计秀
25             Console.ReadLine();  5hb中国设计秀
5hb中国设计秀
26         }  5hb中国设计秀
5hb中国设计秀
27         delegate string AsyncFuncDelegate(int userInput);  5hb中国设计秀
5hb中国设计秀
28         static string Func(int userInput)  5hb中国设计秀
5hb中国设计秀
29         {  5hb中国设计秀
5hb中国设计秀
30             Console.WriteLine("Func start to run");  5hb中国设计秀
5hb中国设计秀
31             Console.WriteLine("...");  5hb中国设计秀
5hb中国设计秀
32             Thread.Sleep(5000);  5hb中国设计秀
5hb中国设计秀
33             Console.WriteLine("Func end to run");  5hb中国设计秀
5hb中国设计秀
34             return userInput.ToString();  5hb中国设计秀
5hb中国设计秀
35         }  5hb中国设计秀
5hb中国设计秀
36     }  5hb中国设计秀
5hb中国设计秀
37 } 5hb中国设计秀
5hb中国设计秀
输出结果如下:5hb中国设计秀
5hb中国设计秀
Implement other tasks 5hb中国设计秀
5hb中国设计秀
Func start to run 5hb中国设计秀
5hb中国设计秀
... 5hb中国设计秀
5hb中国设计秀
Func end to run 5hb中国设计秀
5hb中国设计秀
Implement other tasks end ... 5hb中国设计秀
5hb中国设计秀
Get user's input 5hb中国设计秀
5hb中国设计秀
56 5hb中国设计秀
5hb中国设计秀
1.2 在当前线程中启动线程T,然后继续执行当前线程中的其它任务R1,等待T执行完成,当T执行完成后,继续执行当前线程中的其它任务R2,最后获取T的返回值5hb中国设计秀
view sourceprint?01 static void Main()  5hb中国设计秀
5hb中国设计秀
02         {  5hb中国设计秀
5hb中国设计秀
03             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);  5hb中国设计秀
5hb中国设计秀
04             Console.WriteLine("Input number please...");  5hb中国设计秀
5hb中国设计秀
05             IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), null, null);  5hb中国设计秀
5hb中国设计秀
06             Console.WriteLine("Implement task 1");  5hb中国设计秀
5hb中国设计秀
07             result.AsyncWaitHandle.WaitOne();  5hb中国设计秀
5hb中国设计秀
08             result.AsyncWaitHandle.Close();  5hb中国设计秀
5hb中国设计秀
09             Console.WriteLine("Implment task 2");  5hb中国设计秀
5hb中国设计秀
10             Console.WriteLine("Get user's input");  5hb中国设计秀
5hb中国设计秀
11             Console.WriteLine(caller.EndInvoke(result));  5hb中国设计秀
5hb中国设计秀
12             Console.ReadLine();  5hb中国设计秀
5hb中国设计秀
13         } 5hb中国设计秀
5hb中国设计秀
输出结果如下:5hb中国设计秀
5hb中国设计秀
Input number please... 5hb中国设计秀
5hb中国设计秀
25 5hb中国设计秀
5hb中国设计秀
Implement task 1 5hb中国设计秀
5hb中国设计秀
Func start to run 5hb中国设计秀
5hb中国设计秀
... 5hb中国设计秀
5hb中国设计秀
Func end to run 5hb中国设计秀
5hb中国设计秀
Implment task 2 5hb中国设计秀
5hb中国设计秀
Get user's input 5hb中国设计秀
5hb中国设计秀
25 5hb中国设计秀
5hb中国设计秀
5hb中国设计秀
5hb中国设计秀
1.3 在当前线程中启动线程T,只要T在执行就执行任务R,最后获取T的返回值5hb中国设计秀
view sourceprint?01 [STAThread]  5hb中国设计秀
5hb中国设计秀
02         static void Main()  5hb中国设计秀
5hb中国设计秀
03         {  5hb中国设计秀
5hb中国设计秀
04             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);  5hb中国设计秀
5hb中国设计秀
05             Console.WriteLine("Input number please...");  5hb中国设计秀
5hb中国设计秀
06             IAsyncResult result = caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), null, null);  5hb中国设计秀
5hb中国设计秀
07             while (!result.IsCompleted)  5hb中国设计秀
5hb中国设计秀
08             {  5hb中国设计秀
5hb中国设计秀
09                 Thread.Sleep(1000);  5hb中国设计秀
5hb中国设计秀
10                 Console.Write(">");  5hb中国设计秀
5hb中国设计秀
11             }  5hb中国设计秀
5hb中国设计秀
12             Console.WriteLine("");  5hb中国设计秀
5hb中国设计秀
13             Console.WriteLine("Implement other task2");  5hb中国设计秀
5hb中国设计秀
14             Console.WriteLine("Get user's input");  5hb中国设计秀
5hb中国设计秀
15             Console.WriteLine(caller.EndInvoke(result));  5hb中国设计秀
5hb中国设计秀
16             Console.ReadLine();  5hb中国设计秀
5hb中国设计秀
17         } 5hb中国设计秀
5hb中国设计秀
输出结果如下:5hb中国设计秀
5hb中国设计秀
Func start to run 5hb中国设计秀
5hb中国设计秀
... 5hb中国设计秀
5hb中国设计秀
>>>>>Func end to run 5hb中国设计秀
5hb中国设计秀
> 5hb中国设计秀
5hb中国设计秀
Implement other task2 5hb中国设计秀
5hb中国设计秀
Get user's input 5hb中国设计秀
5hb中国设计秀
23 5hb中国设计秀
5hb中国设计秀
5hb中国设计秀
5hb中国设计秀
2 不需要在当前线程中获取返回值,但是仍然需要对返回值做处理5hb中国设计秀
5hb中国设计秀
5hb中国设计秀
view sourceprint?01 using System;  5hb中国设计秀
5hb中国设计秀
02 using System.Collections.Generic;  5hb中国设计秀
5hb中国设计秀
03 using System.Linq;  5hb中国设计秀
5hb中国设计秀
04 using System.Windows.Forms;  5hb中国设计秀
5hb中国设计秀
05 using System.Threading;  5hb中国设计秀
5hb中国设计秀
06 using System.Runtime.Remoting.Messaging;  5hb中国设计秀
5hb中国设计秀
07 namespace FirstWF  5hb中国设计秀
5hb中国设计秀
08 {  5hb中国设计秀
5hb中国设计秀
09     static class Program  5hb中国设计秀
5hb中国设计秀
10     {  5hb中国设计秀
5hb中国设计秀
11         /// <summary>  5hb中国设计秀
5hb中国设计秀
12         /// The main entry point for the application.  5hb中国设计秀
5hb中国设计秀
13         /// </summary>  5hb中国设计秀
5hb中国设计秀
14         [STAThread]  5hb中国设计秀
5hb中国设计秀
15         static void Main()  5hb中国设计秀
5hb中国设计秀
16         {  5hb中国设计秀
5hb中国设计秀
17             AsyncFuncDelegate caller = new AsyncFuncDelegate(Func);  5hb中国设计秀
5hb中国设计秀
18             Console.WriteLine("Input number please...");  5hb中国设计秀
5hb中国设计秀
19             caller.BeginInvoke(Convert.ToInt32(Console.ReadLine()), new AsyncCallback(CallBackFunc), "Message from Main thread.");  5hb中国设计秀
5hb中国设计秀
20             Console.WriteLine("Main thread ends");  5hb中国设计秀
5hb中国设计秀
21             Console.ReadLine();  5hb中国设计秀
5hb中国设计秀
22         }  5hb中国设计秀
5hb中国设计秀
23         delegate string AsyncFuncDelegate(int userInput);  5hb中国设计秀
5hb中国设计秀
24         static string Func(int userInput)  5hb中国设计秀
5hb中国设计秀
25         {  5hb中国设计秀
5hb中国设计秀
26             Console.WriteLine("Func start to run");  5hb中国设计秀
5hb中国设计秀
27             Console.WriteLine("...");  5hb中国设计秀
5hb中国设计秀
28             Thread.Sleep(5000);  5hb中国设计秀
5hb中国设计秀
29             Console.WriteLine("Func end to run");  5hb中国设计秀
5hb中国设计秀
30             return userInput.ToString();  5hb中国设计秀
5hb中国设计秀
31         }  5hb中国设计秀
5hb中国设计秀
32         static void CallBackFunc(IAsyncResult ar)  5hb中国设计秀
5hb中国设计秀
33         {  5hb中国设计秀
5hb中国设计秀
34             AsyncResult result = ar as AsyncResult;  5hb中国设计秀
5hb中国设计秀
35             string inputMessage = result.AsyncState as string;  5hb中国设计秀
5hb中国设计秀
36             AsyncFuncDelegate caller = result.AsyncDelegate as AsyncFuncDelegate;  5hb中国设计秀
5hb中国设计秀
37             Console.WriteLine("call back starts");  5hb中国设计秀
5hb中国设计秀
38             Console.WriteLine(inputMessage);  5hb中国设计秀
5hb中国设计秀
39             Console.WriteLine("The input number is : " + caller.EndInvoke(ar));  5hb中国设计秀
5hb中国设计秀
40             Console.WriteLine("call back ends");  5hb中国设计秀
5hb中国设计秀
41         }  5hb中国设计秀
5hb中国设计秀
42     }  5hb中国设计秀
5hb中国设计秀
43 } 5hb中国设计秀
5hb中国设计秀
5hb中国设计秀
5hb中国设计秀
输出结果如下:5hb中国设计秀
5hb中国设计秀
Input number please... 5hb中国设计秀
5hb中国设计秀
23 5hb中国设计秀
5hb中国设计秀
Main thread ends 5hb中国设计秀
5hb中国设计秀
Func start to run 5hb中国设计秀
5hb中国设计秀
... 5hb中国设计秀
5hb中国设计秀
Func end to run 5hb中国设计秀
5hb中国设计秀
call back starts 5hb中国设计秀
5hb中国设计秀
Message from Main thread. 5hb中国设计秀
5hb中国设计秀
The input number is : 23 5hb中国设计秀
5hb中国设计秀
call back ends5hb中国设计秀
5hb中国设计秀
5hb中国设计秀
5hb中国设计秀
记得以前的代码,写的都不是很好。虽然call.BeginInvoke可以开始异步调用,但几乎就没有使用过EndInvoke。EndInvoke可以保证异步调用被正常结束,使代码更加健康。5hb中国设计秀
5hb中国设计秀
异步调用,可以使代码具有更高的执行效率,但是在异步调用时,应该有一个健康的使用习惯。5hb中国设计秀
5hb中国设计秀
 5hb中国设计秀

本文引用地址:/bc/article_46125.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明