几种异步操作方式

1、利用线程池发起异步操作

 

View Code
 1 using System;
 2 using System.Threading;
 3 
 4 namespace Asynchronous
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             Console.WriteLine("主线程:准备发起一系列异步操作...");
11             ThreadPool.QueueUserWorkItem(ComputeBoundOp, 5);
12             ThreadPool.QueueUserWorkItem(ComputeBoundOp, 7);
13             Console.WriteLine("主线程:干其它事情...");
14             Thread.Sleep(1000);
15             Console.WriteLine("按回车退出...");
16             Console.ReadLine();
17         }
18 
19         private static void ComputeBoundOp(object o) 
20         {
21             Console.WriteLine("异步操作回调:state={0}", o);
22             Thread.Sleep(1000);
23         }
24     }
25 }

 

2、利用Threading.Tasks中的Task
View Code
 1 using System;
 2 using System.Threading;
 3 using System.Threading.Tasks;
 4 
 5 namespace Asynchronous
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             Console.WriteLine("主线程:准备发起一系列异步操作...");
12             Task t = new Task(ComputeBoundOp,5);
13             t.Start();
14             Console.WriteLine("主线程:干其它事情...");
15             Thread.Sleep(1000);
16             Console.WriteLine("按回车退出...");
17             Console.ReadLine();
18         }
19 
20         private static void ComputeBoundOp(object o) 
21         {
22             Console.WriteLine("异步操作回调:state={0}", o);
23             Thread.Sleep(1000);
24         }
25     }
26 }

3、利用System.Threading.Timer

 

View Code
 1 using System;
 2 using System.Threading;
 3 
 4 
 5 namespace Asynchronous
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             Console.WriteLine("主线程:准备发起一系列异步操作...");
12             Timer t = new Timer(ComputeBoundOp, 5500);
13             Console.WriteLine("主线程:干其它事情...");
14             Thread.Sleep(1000);
15             Console.WriteLine("按回车退出...");
16             Console.ReadLine();
17         }
18 
19         private static void ComputeBoundOp(object o) 
20         {
21             Console.WriteLine("异步操作回调:state={0}", o);
22             Thread.Sleep(1000);
23         }
24     }
25 }

4、利用APM(Asynchronous Programming Model)中的beginXXX方法

 

View Code
 1 using System;
 2 using System.Threading;
 3 
 4 namespace Asynchronous
 5 {
 6     class Program
 7     {
 8         delegate void MyDelegate(object o);
 9 
10         static void Main(string[] args)
11         {
12             Console.WriteLine("主线程:准备发起一系列异步操作...");
13             MyDelegate mydelegate = ComputeBoundOp;
14             mydelegate.BeginInvoke(null,ComputeBoundOpCallBack,5);             
15             Console.WriteLine("主线程:干其它事情...");
16             Thread.Sleep(5000);
17             Console.WriteLine("按回车退出...");
18             Console.ReadLine();
19         }
20 
21         private static void ComputeBoundOp(object o) 
22         {            
23             Thread.Sleep(1000);//模拟异步操作在做一些耗时的操作
24         }
25 
26         private static void ComputeBoundOpCallBack(IAsyncResult ar) 
27         {
28             Console.WriteLine("异步操作的回调:{0}" , ar.AsyncState);
29             
30         }
31     }
32 }

 

posted @ 2011-05-06 10:46  Code & Life  阅读(468)  评论(0编辑  收藏  举报