几种异步操作方式

其实这也是面试中被问倒的问题:(贴在这里纪念一下,注:只是简单的罗列,详细原理及分析,请参阅《CLR Via c#》第三版相关章节)
1、利用线程池发起异步操作
using System;
using System.Threading;

namespace Asynchronous
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("主线程:准备发起一系列异步操作...");
            ThreadPool.QueueUserWorkItem(ComputeBoundOp, 5);
            ThreadPool.QueueUserWorkItem(ComputeBoundOp, 7);
            Console.WriteLine("主线程:干其它事情...");
            Thread.Sleep(1000);
            Console.WriteLine("按回车退出...");
            Console.ReadLine();
        }

        private static void ComputeBoundOp(object o) 
        {
            Console.WriteLine("异步操作回调:state={0}", o);
            Thread.Sleep(1000);
        }
    }
}
结果:
2011030721151984.png
2011030721160977.png
2、利用Threading.Tasks中的Task
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Asynchronous
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("主线程:准备发起一系列异步操作...");
            Task t = new Task(ComputeBoundOp,5);
            t.Start();
            Console.WriteLine("主线程:干其它事情...");
            Thread.Sleep(1000);
            Console.WriteLine("按回车退出...");
            Console.ReadLine();
        }

        private static void ComputeBoundOp(object o) 
        {
            Console.WriteLine("异步操作回调:state={0}", o);
            Thread.Sleep(1000);
        }
    }
}
3、利用System.Threading.Timer
using System;
using System.Threading;


namespace Asynchronous
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("主线程:准备发起一系列异步操作...");
            Timer t = new Timer(ComputeBoundOp, 5, 50, 0);
            Console.WriteLine("主线程:干其它事情...");
            Thread.Sleep(1000);
            Console.WriteLine("按回车退出...");
            Console.ReadLine();
        }

        private static void ComputeBoundOp(object o) 
        {
            Console.WriteLine("异步操作回调:state={0}", o);
            Thread.Sleep(1000);
        }
    }
}
4、利用APM(Asynchronous Programming Model)中的beginXXX方法
using System;
using System.Threading;

namespace Asynchronous
{
    class Program
    {
        delegate void MyDelegate(object o);

        static void Main(string[] args)
        {
            Console.WriteLine("主线程:准备发起一系列异步操作...");
            MyDelegate mydelegate = ComputeBoundOp;
            mydelegate.BeginInvoke(null,ComputeBoundOpCallBack,5);             
            Console.WriteLine("主线程:干其它事情...");
            Thread.Sleep(5000);
            Console.WriteLine("按回车退出...");
            Console.ReadLine();
        }

        private static void ComputeBoundOp(object o) 
        {            
            Thread.Sleep(1000);//模拟异步操作在做一些耗时的操作
        }

        private static void ComputeBoundOpCallBack(IAsyncResult ar) 
        {
            Console.WriteLine("异步操作的回调:{0}" , ar.AsyncState);
            
        }
    }
}
未完待续...

转载于:https://www.cnblogs.com/yjmyzz/archive/2011/03/07/1976356.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值