.net异步调用实现方法 入门级别

.NET异步编程实现方法总结----本文将介绍4种实现

  最近较忙,既要外出出差又要兼顾公司的项目。今天在公司,忙里偷闲,总结一下.NET中的异步调用函数的实现方法,本人在写这篇博文之前自己先动手写了本文的所有示例代码,开写之前是做过功课的,还是用代码说话方有说服力。。。

  本文的内容旨在用最简洁的代码来把异步调用的方法说清楚,有不当之处还请留言多多交流,本人也愿意多多学习~ 好了下面将开始介绍4种实现方法,我在代码中都进行了详细的注释,这里不罗嗦了,直接用代码说明吧O(∩_∩)O

第1种方式:轮询。也是属于“等待”类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 
 7 namespace 异步调用实现方法汇总3
 8 {
 9     /// <summary>
10     /// 异步调用方法总结:
11     /// 3.轮询
12     /// 之前提到的两种方法,只能等下异步方法执行完毕,
13     /// 在完毕之前没有任何提示信息,整个程序就像没有响应一样,用户体验不好,
14     /// 可以通过检查IasyncResult类型的IsCompleted属性来检查异步调用是否完成,
15     /// 如果没有完成,则可以适时地显示一些提示信息
16     /// </summary>
17     class Program
18     {
19         public delegate void PrintDelegate(string s);
20         static void Main(string[] args)
21         {
22             PrintDelegate printDelegate = Print;
23             Console.WriteLine("主线程:"+Thread.CurrentThread.ManagedThreadId );
24             IAsyncResult result = printDelegate.BeginInvoke("Hello world.", null, null);
25             Console.WriteLine("主线程:" + Thread.CurrentThread.ManagedThreadId + ",继续执行...");
26             while (!result.IsCompleted)
27             {
28                 Console.WriteLine(".");
29                 Thread.Sleep(500);
30             }
31 
32             Console.WriteLine("主线程:" + Thread.CurrentThread.ManagedThreadId + "  Press any key to continue...");
33             Console.ReadKey(true);
34         }
35         public static void Print(string s)
36         {
37             Console.WriteLine("当前线程:" + Thread.CurrentThread.ManagedThreadId + s);
38             Thread.Sleep(5000);
39         }
40     }
41 }

需要注意的地方,代码中都有注明了。

第2种方式:WaitOne。同样属于“等待”类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 
 7 namespace 异步调用实现方法汇总2
 8 {
 9     /// <summary>
10     /// 异步调用方法总结:
11     /// 2.WaitOne
12     /// 可以看到,与EndInvoke类似,只是用WaitOne函数代码了EndInvoke而已。
13     /// </summary>
14     class Program
15     {
16         public delegate void PrintDelegate(string s);
17         static void Main(string[] args)
18         {
19             PrintDelegate printDelegate = Print;
20             Console.WriteLine("主线程");
21             IAsyncResult result = printDelegate.BeginInvoke("Hello World.", null, null);
22             Console.WriteLine("主线程继续执行...");
23             result.AsyncWaitHandle.WaitOne(-1, false);
24 
25             Console.WriteLine("Press any key to continue...");
26             Console.ReadKey(true);
27         }
28         public static void Print(string s)
29         {
30             Console.WriteLine("异步线程开始执行:" + s);
31             Thread.Sleep(5000);
32         }
33     }
34 }

第3种方式:BeginEnvoke EndEnvoke方法,属于“等待”类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 
 7 namespace 异步调用实现方法汇总
 8 {
 9     /// <summary>
10     /// 异步调用方法总结:
11     /// 1.BeginEnvoke EndEnvoke
12     /// 当使用BeginInvoke异步调用方法时,如果方法未执行完,EndInvoke方法就会一直阻塞,直到被调用的方法执行完毕
13     /// </summary>
14     class Program
15     {
16         public delegate void PrintDelegate(string s);
17         static void Main(string[] args)
18         {
19             PrintDelegate printDelegate = Print;
20             Console.WriteLine("主线程");
21 
22             IAsyncResult result= printDelegate.BeginInvoke("Hello World.", null, null);
23             Console.WriteLine("主线程继续执行...");
24             //当使用BeginInvoke异步调用方法时,如果方法未执行完,EndInvoke方法就会一直阻塞,直到被调用的方法执行完毕
25             printDelegate.EndInvoke(result);
26 
27             Console.WriteLine("Press any key to continue...");
28             Console.ReadKey(true);
29         }
30 
31         public static void Print(string s)
32         {
33             Console.WriteLine("异步线程开始执行:"+s);
34             Thread.Sleep(5000);
35         }
36     }
37 }

第4种方式:回调。当然属于“回调”类。推荐!!!!

  前面的3种方式在等待异步方法执行完毕后才能拿到执行的结果,在期间主线程均处于等待状态。回调和它们最大的区别是,在调用BeginInvoke时只要提供了回调方法,当日那么主线程就不必要再等待异步线程工作完毕,异步线程在工作结束后会主动调用我们提供的回调方法,并在回调方法中做相应的处理。

 

 1 namespace 异步调用实现方法汇总4
 2 {
 3     /// <summary>
 4     /// 异步调用方法总结:
 5     /// 4.回调
 6     /// 之前三种方法者在等待异步方法执行完毕后才能拿到执行的结果,期间主线程均处于等待状态。
 7     /// 回调和它们最大的区别是,在调用BeginInvoke时只要提供了回调方法,那么主线程就不必要再等待异步线程工作完毕,
 8     /// 异步线程在工作结束后会主动调用我们提供的回调方法,并在回调方法中做相应的处理,例如显示异步调用的结果。
 9     /// </summary>
10     class Program
11     {
12         public delegate void PrintDelegate(string s);
13         static void Main(string[] args)
14         {
15             PrintDelegate printDelegate = Print;
16             Console.WriteLine("主线程.");
17             printDelegate.BeginInvoke("Hello world.", PrintComeplete, printDelegate);
18             Console.WriteLine("主线程继续执行...");
19 
20             Console.WriteLine("Press any key to continue...");
21             Console.ReadKey(true);
22         }
23         public static void Print(string s)
24         { 
25             Console.WriteLine("当前线程:"+s);
26             Thread.Sleep(5000);
27         }
28         //回调方法要求
29         //1.返回类型为void
30         //2.只有一个参数IAsyncResult
31         public static void PrintComeplete(IAsyncResult result)
32         {
33             (result.AsyncState as PrintDelegate).EndInvoke(result);
34             Console.WriteLine("当前线程结束." + result.AsyncState.ToString());
35         }
36     }
37 }

以上就是4种实现异步调用函数的4种方式,说的很清楚了,就写这么多~欢迎大家多多留言交流,编程这东西需要多多交流,多多写代码。

下面总结下异步调用的实质

异步调用的实质:
异步调用通过委托将所需调用的方法置于一个新线程上运行,从而能够使一个可能需要较长时间的任务在后台执行而不影响调用方的其他行为。

异步调用的实现:
前面已经讲道,异步调用通过委托实现。委托支持同步和异步调用。在同步调用中,一个委托的实例可记录多个目标方法;在异步调用中,一个委托实例中有且只能包含一个目标方法。异步调用使用委托实例的BeginInvoke方法和EndInvoke方法分别开始调用和检索返回值,这两个方法在编译期生成。调用BeginInvoke后委托立即返回;调用EndInvoke时倘若委托方法未执行完毕,则阻塞当前线程至调用完毕。

好了,就到这里吧,转载请附上网址:http://www.bjp111.com/zymianmo.aspx

posted @ 2013-04-18 14:31  leyoutt  阅读(292)  评论(0)    收藏  举报