1: using System;
2: using System.Threading;
3: class Program
4: {
5: static void Main(string[] args)
6: {
7: Func<int, int, int> agent = new Func<int, int, int>(TakeAWhile);
8:
9: //委托使用线程池来完成异步任务
10: IAsyncResult iAsync = agent.BeginInvoke(1, 3000, null, null);
11:
12: while (!iAsync.IsCompleted)
13: {
14: Console.Write(".");
15: System.Threading.Thread.Sleep(50);
16: }
17:
18: int result = agent.EndInvoke(iAsync);
19:
20: Console.WriteLine("result:{0}", result);
21:
22: Console.ReadLine();
23: }
24:
25: static int TakeAWhile(int data, int ms)
26: {
27: Console.WriteLine("TakesAWhile started");
28:
29: System.Threading.Thread.Sleep(ms);
30:
31: Console.WriteLine("TakesAWhile completed");
32: return ++data;
33: }
34: }