惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - RobotTech

[Z] 从Uncaught SyntaxError: Unexpected token ")" 问题看javascript:void的作用 [Z] C#程序中设置全局代理(Global Proxy) [Z] SQL SERVER 的前世今生--各版本功能对比 关于.NET编译的目标平台(AnyCPU,x86,x64) (转) [转] HTTP Headers 入门 [转] 一个小时学会Git 用命令编译 js事件之event.preventDefault()与event.stopPropagation()用法区别 [转] The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing [转] Visual Studio Code behind a proxy [转] js == 与 === 的区别 Oracle 12c SQL Server死锁 Initialize the Storage Emulator by Using the Command-Line Tool Microsoft Fakes Identifier 'Logic.DomainObjectBase._isNew' is not CLS-compliant SSD TRIM Visual Studio 2013 prerequisites 怎样用UltraISO制作U盘系统安装盘
[转]说说C#的async和await
RobotTech · 2016-07-07 · via 博客园 - RobotTech

C# 5.0中引入了async 和 await。这两个关键字可以让你更方便的写出异步代码。

看个例子:

  1. public class MyClass  
  2. {  
  3.     public MyClass()  
  4.     {  
  5.         DisplayValue(); 
  6.         System.Diagnostics.Debug.WriteLine("MyClass() End.");  
  7.     }  
  8.     public Task<double> GetValueAsync(double num1, double num2)  
  9.     {  
  10.         return Task.Run(() =>  
  11.         {  
  12.             for (int i = 0; i < 1000000; i++)  
  13.             {  
  14.                 num1 = num1 / num2;  
  15.             }  
  16.             return num1;  
  17.         });  
  18.     }  
  19.     public async void DisplayValue()  
  20.     {  
  21.         double result = await GetValueAsync(1234.5, 1.01);
  22.         
  23.         System.Diagnostics.Debug.WriteLine("Value is : " + result);  
  24.     }  
  25. }  

上面在MyClass的构造函数里调用了async关键字标记的异步方法DisplayValue(),DisplayValue()方法里执行了一个await关键字标记的异步任务GetValueAsync(),这个异步任务必须是以Task或者Task<TResult>作为返回值的,而我们也看到,异步任务执行完成时实际返回的类型是void或者TResult,DisplayValue()方法里await GetValueAsync()之后的所有代码都会在异步任务完成时才会执行。

DisplayValue()方法实际执行的代码如下:

  1. public void DisplayValue()  
  2. {  
  3.     System.Runtime.CompilerServices.TaskAwaiter<double> awaiter = GetValueAsync(1234.5, 1.01).GetAwaiter();  
  4.     awaiter.OnCompleted(() =>  
  5.         {  
  6.             double result = awaiter.GetResult();  
  7.             System.Diagnostics.Debug.WriteLine("Value is : " + result);  
  8.         });  
  9. }   


可以看到,async和await关键字只是把上面的代码变得更简单易懂而已。

程序的输出如下:

MyClass() End.

Value is : 2.47032822920623E-322

以下是我写的一个静态类,可以方便将一个普通Function执行异步调用:

  1. public static class TaskAsyncHelper  
  2. {  
  3.     
  4.     
  5.     
  6.     
  7.     
  8.     public static async void RunAsync(Action function, Action callback)  
  9.     {  
  10.         Func<System.Threading.Tasks.Task> taskFunc = () =>  
  11.         {  
  12.             return System.Threading.Tasks.Task.Run(() =>  
  13.             {  
  14.                 function();  
  15.             });  
  16.         };  
  17.         await taskFunc();  
  18.         if (callback != null)  
  19.             callback();  
  20.     }  
  21.   
  22.     
  23.     
  24.     
  25.     
  26.     
  27.     
  28.     public static async void RunAsync<TResult>(Func<TResult> function, Action<TResult> callback)  
  29.     {  
  30.         Func<System.Threading.Tasks.Task<TResult>> taskFunc = ()=>  
  31.             {  
  32.                 return System.Threading.Tasks.Task.Run(()=>  
  33.                     {  
  34.                         return function();  
  35.                     });  
  36.             };  
  37.         TResult rlt = await taskFunc();  
  38.         if(callback != null)  
  39.             callback(rlt);  
  40.     }  
  41. }  


使用很简单,将方法名作为参数传进去就行了,最常用的是把很耗时的序列化函数传进去,以免阻塞UI进程,造成卡顿现象,影响用户体验。