根据Chris Sells的代码修改
英文版原作者:Chris Sells(http://www.sellsbrothers.com/writing/default.aspx?content=delegates.htm)
The following is an excerpt from Windows Forms 2.0 Programming
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading;
5
6namespace AsyncDelegate
7{
8
9
10 delegate void WorkStarted();
11 delegate void WorkProgressing();
12 delegate int WorkCompleted();
13
14 class Worker
15 {
16 public event WorkStarted Started;
17 public event WorkProgressing Progressing;
18 public event WorkCompleted Completed;
19 private static string _name;
20
21 public Worker(string name)
22 {
23 _name = name;
24 }
25 public void DoWork()
26 {
27
28
29 if (this.Started != null)
30 this.Started();
31 ConsoleOutPut("started");
32
33 ConsoleOutPut("progressing");
34
35 if (this.Progressing != null)
36 this.Progressing();
37 ConsoleOutPut("completed");
38
39
40 if (this.Completed != null)
41 {
42 foreach (WorkCompleted wc in this.Completed.GetInvocationList())
43 {
44 WorkCompleted wc2 = wc;
45 AsyncCallback callback=delegate(IAsyncResult result)
46 {
47
48
49 int grade = wc2.EndInvoke(result);
50 Console.WriteLine("Worker grade= {0}", grade);
51
52
53 };
54 IAsyncResult ar = wc.BeginInvoke(callback, null);
55 //当异步调用开始但未完成时做UI状态模拟处理
56 while (!ar.IsCompleted)
57 {
58
59
60
61 Console.WriteLine("Worker grade is processing");
62
63 Thread.Sleep(1000);
64
65 }
66 }
67 }
68 }
69
70 public static void ConsoleOutPut(string endcontext)
71 {
72 Console.Write("Worker ");
73 Console.ForegroundColor = ConsoleColor.Cyan;
74 Console.Write("{0}", _name);
75 Console.ForegroundColor = ConsoleColor.DarkGreen;
76 Console.WriteLine(":work {0}", endcontext);
77 }
78
79 public static void ConsoleOutPut(string precontext, string endcontext)
80 {
81
82 Console.Write("{0} ", precontext);
83 Console.ForegroundColor = ConsoleColor.Cyan;
84 Console.Write("{0}", _name);
85 Console.ForegroundColor = ConsoleColor.DarkGreen;
86 Console.WriteLine(" {0}", endcontext);
87 }
88 }
89
90 class Boss
91 {
92 public int WorkCompleted()
93 {
94 System.Threading.Thread.Sleep(2000);
95 Console.WriteLine("Better");
96 return 5;
97 }
98 }
99
100 class Universe
101 {
102 void WorkerStartedWork()
103 {
104 Console.WriteLine("Universe notices worker starting work");
105 }
106
107 static int WorkerCompletedWork()
108 {
109 System.Threading.Thread.Sleep(3000);
110 Console.WriteLine("Universe pleased with worker's work");
111 return 7;
112 }
113
114 static void Main()
115 {
116 String name = "";
117 Console.Title = "控制台测试异步委托";
118 Console.ForegroundColor = ConsoleColor.DarkGreen;
119 Console.Write("Please enter worker's name here:");
120 Console.ForegroundColor = ConsoleColor.Magenta;
121 name = Console.ReadLine();
122 Console.ForegroundColor = ConsoleColor.DarkGreen;
123 Console.BackgroundColor = ConsoleColor.Black;
124 Console.Beep();
125
126
127 Worker peter = new Worker(name);
128 Worker.ConsoleOutPut("Hello!", "Good Morning!");
129 Boss boss = new Boss();
130 Universe u = new Universe();
131 peter.Completed += boss.WorkCompleted;
132 peter.Started += u.WorkerStartedWork;
133 peter.Completed += Universe.WorkerCompletedWork;
134 peter.DoWork();
135
136 Console.WriteLine("Main: worker completed work");
137 Console.Read();
138 }
139 }
140
141
142
143}
144
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading;
5
6namespace AsyncDelegate
7{
8
9
10 delegate void WorkStarted();
11 delegate void WorkProgressing();
12 delegate int WorkCompleted();
13
14 class Worker
15 {
16 public event WorkStarted Started;
17 public event WorkProgressing Progressing;
18 public event WorkCompleted Completed;
19 private static string _name;
20
21 public Worker(string name)
22 {
23 _name = name;
24 }
25 public void DoWork()
26 {
27
28
29 if (this.Started != null)
30 this.Started();
31 ConsoleOutPut("started");
32
33 ConsoleOutPut("progressing");
34
35 if (this.Progressing != null)
36 this.Progressing();
37 ConsoleOutPut("completed");
38
39
40 if (this.Completed != null)
41 {
42 foreach (WorkCompleted wc in this.Completed.GetInvocationList())
43 {
44 WorkCompleted wc2 = wc;
45 AsyncCallback callback=delegate(IAsyncResult result)
46 {
47
48
49 int grade = wc2.EndInvoke(result);
50 Console.WriteLine("Worker grade= {0}", grade);
51
52
53 };
54 IAsyncResult ar = wc.BeginInvoke(callback, null);
55 //当异步调用开始但未完成时做UI状态模拟处理
56 while (!ar.IsCompleted)
57 {
58
59
60
61 Console.WriteLine("Worker grade is processing");
62
63 Thread.Sleep(1000);
64
65 }
66 }
67 }
68 }
69
70 public static void ConsoleOutPut(string endcontext)
71 {
72 Console.Write("Worker ");
73 Console.ForegroundColor = ConsoleColor.Cyan;
74 Console.Write("{0}", _name);
75 Console.ForegroundColor = ConsoleColor.DarkGreen;
76 Console.WriteLine(":work {0}", endcontext);
77 }
78
79 public static void ConsoleOutPut(string precontext, string endcontext)
80 {
81
82 Console.Write("{0} ", precontext);
83 Console.ForegroundColor = ConsoleColor.Cyan;
84 Console.Write("{0}", _name);
85 Console.ForegroundColor = ConsoleColor.DarkGreen;
86 Console.WriteLine(" {0}", endcontext);
87 }
88 }
89
90 class Boss
91 {
92 public int WorkCompleted()
93 {
94 System.Threading.Thread.Sleep(2000);
95 Console.WriteLine("Better");
96 return 5;
97 }
98 }
99
100 class Universe
101 {
102 void WorkerStartedWork()
103 {
104 Console.WriteLine("Universe notices worker starting work");
105 }
106
107 static int WorkerCompletedWork()
108 {
109 System.Threading.Thread.Sleep(3000);
110 Console.WriteLine("Universe pleased with worker's work");
111 return 7;
112 }
113
114 static void Main()
115 {
116 String name = "";
117 Console.Title = "控制台测试异步委托";
118 Console.ForegroundColor = ConsoleColor.DarkGreen;
119 Console.Write("Please enter worker's name here:");
120 Console.ForegroundColor = ConsoleColor.Magenta;
121 name = Console.ReadLine();
122 Console.ForegroundColor = ConsoleColor.DarkGreen;
123 Console.BackgroundColor = ConsoleColor.Black;
124 Console.Beep();
125
126
127 Worker peter = new Worker(name);
128 Worker.ConsoleOutPut("Hello!", "Good Morning!");
129 Boss boss = new Boss();
130 Universe u = new Universe();
131 peter.Completed += boss.WorkCompleted;
132 peter.Started += u.WorkerStartedWork;
133 peter.Completed += Universe.WorkerCompletedWork;
134 peter.DoWork();
135
136 Console.WriteLine("Main: worker completed work");
137 Console.Read();
138 }
139 }
140
141
142
143}
144