C#--async-await的用法
2016-08-04 19:54
543 查看
NET 中的 async/await 异步编程
MSDN上的文章
**async与await最佳实践
需要先理解thread的用法,再掌握task的用法,才能力理解async和await的用法
MSDN上的文章
**async与await最佳实践
需要先理解thread的用法,再掌握task的用法,才能力理解async和await的用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Threading; using System.Threading.Tasks; namespace TestApp { class Program { static void Main(string[] args) { Console.WriteLine("-------主线程启动-------"); Task<int> task = GetLengthAsync(); Console.WriteLine("Main方法做其他事情"); Console.WriteLine("Task返回的值" + task.Result); Console.WriteLine("-------主线程结束-------"); } static Task<int> GetLengthAsync() { Console.WriteLine("GetLengthAsync Start"); Task<int> task = Task<int>.Run(() => { string str = GetStringAsync().Result; Console.WriteLine("GetLengthAsync End"); return str.Length; }); return task; } static Task<string> GetStringAsync() { return Task<string>.Run(() => { Thread.Sleep(2000); return "finished"; }); } } }
相关文章推荐
- C#入门5.9——跳转语句之break语句
- C#中字符和字符串总结
- Vs2013 快捷键
- C# 虚方法(virtual)
- C#入门5.7——迭代语句之foreach语句
- C#中char[]与string之间的转换
- C# GZip对字符串压缩和解压
- C# 16进制与字符串、字节数组之间的转换
- C# 从服务器下载文件代码
- C# 线程通信 一
- C# static、 const和readonly区别
- C# static、 const和readonly区别
- C# static、 const和readonly区别
- C# static、 const和readonly区别
- C# static、 const和readonly区别
- C# static、 const和readonly区别
- C# static、 const和readonly区别
- C# static、 const和readonly区别
- C# static、 const和readonly区别
- C# static、 const和readonly区别