几种异步操作方式

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

 

ExpandedBlockStart.gif 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
ExpandedBlockStart.gif 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

 

ExpandedBlockStart.gif 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,  5 50 0 );
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方法

 

ExpandedBlockStart.gif 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  }

 

转载于:https://www.cnblogs.com/mybluesky99/archive/2011/05/06/2038622.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值