抽象出一个炒菜的类。
1: //炒菜
2: public class Cooking
3: {
4: public string Cook(string food1, string food2)
5: {
6: Thread.CurrentThread.Name = "王大厨";
7: Console.WriteLine("厨师开始炒菜");
8:
9: for (int i = 1; i <= 2; i++)
10: {
11: Thread.Sleep(TimeSpan.FromSeconds(i));
12: Console.WriteLine("{0}:在炒菜(时间:{1}秒)",Thread.CurrentThread.Name,i);
13: }
14: Console.WriteLine("好了,菜做完了");
15: return food1 + food2;
16: }
17: }
客户端调用该类和方法,只有等炒菜线程执行完才能执行后面。
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: Console.WriteLine("客人点菜了!");
6:
7: Cooking cooking = new Cooking();
8: string result = cooking.Cook("鱼香", "肉丝");
9: Console.WriteLine("{0}好了,请慢用。", result);
10:
11: Console.WriteLine("客人吃饭");
12: Console.ReadKey();
13: }
14: }
我们想让客人在炒菜上来之前喝点水,嗑点瓜子......
那就把炒菜的事交给委托,客人就可以放心地做其它事了。
使用委托异步调用注册方法
1: using System.Threading;
2: using System.Threading.Tasks;
3:
4: namespace ConsoleApplication17
5: {
6: class Program
7: {
8: static void Main(string[] args)
9: {
10: Console.WriteLine("客人点菜了!");
11:
12: Cooking cooking = new Cooking();
13: CookDelegate del = new CookDelegate(cooking.Cook); //给委托注册方法
14: IAsyncResult asyncResult = del.BeginInvoke("西红柿", "鸡蛋", null, null);
15: Console.WriteLine("客人做点其它事......");
16: string result = del.EndInvoke(asyncResult);
17: Console.WriteLine("{0}好了,请慢用。", result);
18: Console.ReadKey();
19: }
20: }
21:
22: public delegate string CookDelegate(string food1, string food2);
23:
24: //炒菜
25: public class Cooking
26: {
27: public string Cook(string food1, string food2)
28: {
29: Thread.CurrentThread.Name = "王大厨";
30: Console.WriteLine("厨师开始炒菜");
31:
32: for (int i = 1; i <= 2; i++)
33: {
34: Thread.Sleep(TimeSpan.FromSeconds(i));
35: Console.WriteLine("{0}:在炒菜(时间:{1}秒)",Thread.CurrentThread.Name,i);
36: }
37: Console.WriteLine("好了,菜做完了");
38: return food1 + food2;
39: }
40: }
41: }
42:
委托异步调用注册方法结束后调用回调方法
1: using System.Runtime.Remoting.Messaging;
2: using System.Text;
3: using System.Threading;
4: using System.Threading.Tasks;
5:
6: namespace ConsoleApplication17
7: {
8: class Program
9: {
10: static void Main(string[] args)
11: {
12: Console.WriteLine("客人点菜了!");
13:
14: Cooking cooking = new Cooking();
15: CookDelegate del = new CookDelegate(cooking.Cook); //给委托注册方法
16:
17: string data = "希望您能喜欢";
18: AsyncCallback callBack = new AsyncCallback(OnCookComplete);
19: IAsyncResult asyncResult = del.BeginInvoke("西红柿", "鸡蛋", callBack, data);
20: Console.WriteLine("客人做点其它事......");
21:
22: Console.ReadKey();
23: }
24:
25: //回调需要执行的方法
26: static void OnCookComplete(IAsyncResult asyncResult)
27: {
28: AsyncResult result = (AsyncResult) asyncResult;
29:
30: //从异步结果中可以拿到委托
31: CookDelegate del = (CookDelegate)result.AsyncDelegate;
32:
33: //从异步结果中可以拿到BeginInvoke方法中的object参数
34: string data = (string) asyncResult.AsyncState;
35:
36: string r = del.EndInvoke(asyncResult);
37: Console.WriteLine("{0}好了,请慢用,{1}", r,data);
38: }
39: }
40:
41: public delegate string CookDelegate(string food1, string food2);
42:
43: //炒菜
44: public class Cooking
45: {
46: public string Cook(string food1, string food2)
47: {
48: Thread.CurrentThread.Name = "王大厨";
49: Console.WriteLine("厨师开始炒菜");
50:
51: for (int i = 1; i <= 2; i++)
52: {
53: Thread.Sleep(TimeSpan.FromSeconds(i));
54: Console.WriteLine("{0}:在炒菜(时间:{1}秒)",Thread.CurrentThread.Name,i);
55: }
56: Console.WriteLine("好了,菜做完了");
57: return food1 + food2;
58: }
59: }
60: }
61:
可见:
● 当使用委托异步调用注册的方法时,不会影响到客户端,客户端程序不会等
● 当异步调用委托注册的方法时,参数不仅有方法参数,还有AsyncCallback回调参数,和object对象
● 回调方法中,可以从IAsyncResult获取方法返回值,object对象