async和await异步操作
2015-06-30 15:13
561 查看
class Program { static void Main(string[] args) { // 同步方式 Console.WriteLine("异步方式测试开始!"); AstbcMethod(35); Console.WriteLine("异步方式结束!"); Console.ReadKey(); } private static async void AstbcMethod(int input) { Console.WriteLine("进入异步操作!"); var result = await AsyncWork(input); Console.WriteLine("最终结果{0}", result); Console.WriteLine("退出异步操作!"); } private static async Task<int> AsyncWork(int val) { for (int i = val; i < 40; i++) { Console.WriteLine("耗时操作{0}", i); var result = await Task.Factory.StartNew((Func<object, int>)Fn, i); Console.WriteLine("耗时操作j结果{0}", result); val++; } return val; } //斐波那契数列 private static int Fn(object n) { var i = (int) n; if (i <= 0) { throw new ArgumentOutOfRangeException(); } if (i == 1 || i == 2) { return 1; } return checked(Fn(i - 1) + Fn(i - 2)); // when n>46 memory will overflow } }
相关文章推荐
- .NET4.5之初识async与await
- AIX 减少缺省分页空间(hd6)的大小
- selenium wait
- Leetcode 182 Duplicate Emails
- raid技术-研究感受
- org.tinygroup.beancontainer-IOC、AOP框架
- Container With Most Water——LeetCode
- constraints添加工具&&其他有用博客
- 使用AIDL实现进程间的通信
- 估算Baidu和Google的网页索引数量之比
- 三种方法解决 Failed to start LSB: Bring up/down networking 问题
- 预览时显示Failed to find style buttonStyle 报错,预览界面字体小
- Leetcode 217 Contains Duplicate
- rails断点调试
- Leetcode 219 Contains Duplicate II
- [MFC] 关于OnPaint()
- Contains Duplicate II
- maven项目配置Project Facets时further configuration available不出来问题
- AI中的几种搜索算法---A*搜索算法
- AI中的几种搜索算法---SA搜索算法