.net 4.0的Task已经让我们可以非常简单地使用多线程,并且可以有返回值,也可以支持线程的取消等操作,可谓已经很强大了。但.net 4.5为我们带来了async&await,使得实现多线程的写法更简单,更优美,更符合线性思维。
下面通过一个例子来演示通过Task和async&await分别如何实现,并且最后还附上代码执行顺序图。
使用Task实现
如下代码:
1 #region 使用Task实现 2 static void TestByTask() 3 { 4 Console.WriteLine("main thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId); 5 var task = Task.Factory.StartNew<string>(() => 6 { 7 return GetNameByTask(); 8 }); 9 Console.WriteLine("get another thread result,result:" + task.Result); 10 Console.WriteLine("main thread completed!"); 11 } 12 13 static string GetNameByTask() 14 { 15 Console.WriteLine("another thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId); 16 return "mcgrady"; 17 } 18 #endregion
输出结果:
使用async&await实现
假如使用async&await如何实现呢,如下代码:
1 #region 使用async&await实现 2 static async void TestByAsyncAwait() 3 { 4 Console.WriteLine("main thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId); 5 var name = GetNameByAsyncAwait(); 6 7 Console.WriteLine(string.Format("get another thread result,result:{0}", await name)); 8 Console.WriteLine("main thread completed!"); 9 } 10 11 static async Task<string> GetNameByAsyncAwait() 12 { 13 return await Task.Factory.StartNew<string>(() => 14 { 15 Console.WriteLine("another thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId); 16 return "mcgrady"; 17 }); 18 } 19 #endregion
输出结果:
输出结果跟使用Task相同。
代码执行流程如下图:
完整代码:

1 namespace ConsoleApplication25 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 //1,使用task实现 8 //TestByTask(); 9 10 //2,使用async&await实现 11 TestByAsyncAwait(); 12 13 Console.ReadKey(); 14 } 15 16 #region 使用Task实现 17 static void TestByTask() 18 { 19 Console.WriteLine("main thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId); 20 var task = Task.Factory.StartNew<string>(() => 21 { 22 return GetNameByTask(); 23 }); 24 Console.WriteLine("get another thread result,result:" + task.Result); 25 Console.WriteLine("main thread completed!"); 26 } 27 28 static string GetNameByTask() 29 { 30 Console.WriteLine("another thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId); 31 return "mcgrady"; 32 } 33 #endregion 34 35 #region 使用async&await实现 36 static async void TestByAsyncAwait() 37 { 38 Console.WriteLine("main thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId); 39 var name = GetNameByAsyncAwait(); 40 41 Console.WriteLine(string.Format("get another thread result,result:{0}", await name)); 42 Console.WriteLine("main thread completed!"); 43 } 44 45 static async Task<string> GetNameByAsyncAwait() 46 { 47 return await Task.Factory.StartNew<string>(() => 48 { 49 Console.WriteLine("another thread start,current thread id:" + Thread.CurrentThread.ManagedThreadId); 50 return "mcgrady"; 51 }); 52 } 53 #endregion 54 } 55 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架