异步委托

无回调函数的异步委托:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Threading;
 7 
 8 namespace 异步委托
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             Func<int, int, string> delFunc = (a, b) => 
15             {                
16                 return (a + b).ToString();
17             };
18             //异步调用委托
19             IAsyncResult result = delFunc.BeginInvoke(3, 4, null, null); //内部原理:使用了一个线程池的线程去执行了委托执向的方法
20             //获取异步委托的结果
21             if (result.IsCompleted) //判断异步委托是否执行完成,返回布尔值
22             {
23                 string str = delFunc.EndInvoke(result); //EndInvoke方法会阻塞当前的线程,直到异步委托执行完成之后,才能继续往下执行
24                 Console.WriteLine(str);
25             }
26             Console.ReadKey();
27         }
28     }
29 }

 有回调函数的异步委托:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Threading;
 7 using System.Runtime.Remoting.Messaging;
 8 
 9 namespace 异步委托
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             Func<int, int, string> delFunc = (a, b) => 
16             {                
17                 return (a + b).ToString();
18             };
19             ////异步调用委托
20             //IAsyncResult result = delFunc.BeginInvoke(3, 4, null, null); //内部原理:使用了一个线程池的线程去执行了委托执向的方法
21             ////获取异步委托的结果
22             //if (result.IsCompleted) //判断异步委托是否执行完成,返回布尔值
23             //{
24             //    string str = delFunc.EndInvoke(result); //EndInvoke方法会阻塞当前的线程,直到异步委托执行完成之后,才能继续往下执行
25             //    Console.WriteLine(str);
26             //}
27             delFunc.BeginInvoke(3, 4, MyAsyncCallback, "123");
28             Console.ReadKey();
29         }
30         //回调函数:是异步委托方法执行完成之后,再来调,回调函数
31         public static void MyAsyncCallback(IAsyncResult ar)
32         {
33             //1、拿异步委托执行的结果
34             AsyncResult result = (AsyncResult)ar;
35             var del =(Func<int, int, string>)result.AsyncDelegate; //拿到委托的实例:delFunc
36             string returnValue = del.EndInvoke(result);
37             Console.WriteLine("返回值:"+returnValue);
38             //2、拿到给回调函数的参数
39             Console.WriteLine("传给异步回调函数的参数:"+result.AsyncState); 
40         }
41     }
42 }

效果

有回调函数更优方法:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Threading;
 7 using System.Runtime.Remoting.Messaging;
 8 
 9 namespace 异步委托
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             Func<int, int, string> delFunc = (a, b) => 
16             {                
17                 return (a + b).ToString();
18             };
19             delFunc.BeginInvoke(3, 4, MyAsyncCallback, delFunc);
20             Console.ReadKey();
21         }
22         //回调函数:是异步委托方法执行完成之后,再来调,回调函数
23         public static void MyAsyncCallback(IAsyncResult ar)
24         {
25             var del= (Func<int, int, string>)ar.AsyncState;
26             string returnValue = del.EndInvoke(ar);
27             Console.WriteLine("返回值:" + returnValue);
28             Console.WriteLine("传给异步回调函数的参数:" + ar.AsyncState);
29         }
30     }
31 }

 

posted @ 2019-06-18 00:24  陈彦斌  阅读(209)  评论(0编辑  收藏  举报