Serviceboy 发表于 2013-1-3 15:33:04

如何在VS2010中使用Async功能?

<div id="cnblogs_post_body">伴随C#5.0的发布,“异步”特性越来越深入人心;在VS2012中早就可以使用它大大简化异步编程的痛苦,那么在VS2010中呢?我们无法尝鲜么?答案是“No”!,其实我们可以这样做:
1)必须把你的VS2010更新到SP1,没有请下载此SP1包:http://download.microsoft.com/download/E/B/A/EBA0A152-F426-47E6-9E3F-EFB686E3CA20/VS2010SP1dvd1.iso
2)必须下载此安装程序:http://www.microsoft.com/en-us/download/details.aspx?id=9983
3)删除以下补丁(到控制面板自己搜):KB2635973, KB2615527, KB2645410(具体参考:http://blogs.msdn.com/b/lucian/archive/2012/03/25/asyncctp-installation-problems-and-vs11.aspx)
4)“开始”=>“文档”中找到“示例代码Sample”文件夹,然后在VS中引用“AsyncCtpLibrary.dll”即可啦!以后即便打上这些补丁,貌似还是可以正常工作滴!
为了庆祝,特地献上简短的异步代码:


<div class="cnblogs_code">namespace CSharp{    public class MainTest    {      private static int Pfun(object num)      {            int i = (int)num;            if (i == 0 || i == 1) return 1;            return i * Pfun(i-1);      }      public staticasync Task<int> Fun(int num)      {            return await Task<int>.Factory.StartNew(new Func<object, int>(Pfun), num);      }      static async void Main(string[] args)      {            int result = await Fun(11);            Console.WriteLine(result);      }    }}
页: [1]
查看完整版本: 如何在VS2010中使用Async功能?