.net async await
2016-06-27 17:42
267 查看
using System; using System.Net.Http; using System.Threading; using System.Threading.Tasks; namespace CookBook { class Program { static void Main(string[] args) { //Class1 class1 = new Class1(); //Console.WriteLine("主线程开始"); //class1.DoSomethingAsync(); //Console.WriteLine("主线程结束"); //Class2 class2 = new Class2(); //Console.WriteLine("主线程开始"); //class2.DoSomethingAsync(); //Console.WriteLine("主线程结束"); //string info = Class3.GetUrlStr("http://www.baidu.com"); //Console.WriteLine(info); Console.WriteLine("1主线程开始:{0}", Thread.CurrentThread.ManagedThreadId); Program p = new Program(); string url = "http://www.baidu.com"; int timeout = 1; p.DoSomethingAsync(url, timeout); p.DoSomething(); Console.WriteLine("6主线程结束:{0}", Thread.CurrentThread.ManagedThreadId); Console.ReadKey(); } public async Task DoSomethingAsync(string url, int timeout) { Console.WriteLine("2子线程开始:{0}", Thread.CurrentThread.ManagedThreadId); try { var result = await DownloadStringWithRetries(url, timeout); Console.WriteLine("{0}", result); } catch (Exception ex) { Console.WriteLine("7错误:{1},{0}", Thread.CurrentThread.ManagedThreadId, ex.Message); } Console.WriteLine("4子线程结束:{0}", Thread.CurrentThread.ManagedThreadId); } private void DoSomething() { Console.WriteLine("3主线程开始:{0}", Thread.CurrentThread.ManagedThreadId); for (int j = 0; j != 1000; j++) { int result = 0; for (int i = 0; i != 10000000; i++) { result *= i; } } Console.WriteLine("5主线程开始:{0}", Thread.CurrentThread.ManagedThreadId); } /// <summary> /// 获取url源码,报错重试 /// </summary> /// <param name="url"></param> /// <param name="timeout"></param> /// <returns></returns> private async Task<string> DownloadStringWithRetries(string url, int timeout) { var nextDelay = TimeSpan.FromSeconds(1); for (int i = 0; i != 3; ++i) { try { return await DownloadStringWithTimeout(url, timeout); } catch { } await Task.Delay(nextDelay); nextDelay = nextDelay + nextDelay; } return await DownloadStringWithTimeout(url, timeout); } /// <summary> /// 获取url源码,超时直接报错 /// </summary> /// <param name="url"></param> /// <param name="timeout"></param> /// <returns></returns> private async Task<string> DownloadStringWithTimeout(string url, int timeout) { using (var client = new HttpClient()) { var downloadTask = client.GetStringAsync(url); var timeoutTask = Task.Delay(timeout); var completedTask = await Task.WhenAny(downloadTask, timeoutTask); if (completedTask == timeoutTask) { throw new Exception("处理超时"); } return await downloadTask; } } } }
相关文章推荐
- win10 Failed to open/create the internal network 'HostInterfaceNetworking-VirtualBox Host
- webstrom Certificate validation failed
- 2016.6.27笔记(1)-多线程的wait,notify
- Leetcode-container-with-most-water
- http://blog.csdn.net/wangkehuai/article/details/45057811
- win8系统蓝屏提示DRIVER POWER STATE FAILURE的原因及两种解决方法
- POJ1273 Drainage Ditches 【最大流】
- Leetcode-climbing-stairs
- svn post-commit hook faild(exit code 1) with output /Username not found 错误
- Paint之函数大汇总
- activemq调优
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
- LightOJ 1038 Race to 1 Again
- 【error】LINK1123: failure during conversion to COFF: file invalid or corrupt
- Mongo运行错误:Failed to connect 127.0.0.1:27017,reason:errno:10061由于目标计算机积极拒绝,无法连接
- 【NOIP2013模拟联考5】军训(training)
- Memblaze发布企业级数据存储管理系统FlashRAID——从NVMe SSD出发,全面进击软件定义存储
- saiku源码整合(无maven情况下)
- ssrs 2016, mobile report error: The report may be misconfigured, the data may not be available, or the server version may be unsupported.
- ADB server didn't ack * failed to start daemon及unable to obtain result of 'adb versio错误