C# await和async

基础阅读:http://www.cnblogs.com/jesse2013/p/async-and-await.html

答疑阅读:http://www.cnblogs.com/heyuquan/archive/2012/11/30/async-and-await-faq.html#3467736

总结:

0.async修饰的方法只能为void,Task或Task<T>

1.使用async的方法不一定会强制异步

2.await关键字是针对Task的,为了挂起当前线程,使得Task执行完毕后,再继续当前线程的执行;

3.await必须要在有async修复的方法体中,所以不难发现,如果你看到了await关键字,那么

   async和Task你也一定能见到。

4.简单理解,await 修饰一个Task时是为了得到某种具体的结果;

var r1=await Task.run(()=>MyMethod1());//r1 is int
var r2=await Task.run(()=>MyMethod2());//wrong Syntaxes
var r3=Task.run(()=>MyMethod1());         //r3 is a Task,r3.Result can be called
var r4= Task.run(()=>MyMethod2());        //r4 is a Task,r4 has no Result attribute

public int MyMethod1()
{
    Thread.Sleep(2000);  
}

public void MyMethod2()
{
    Thread.Sleep(2000);  
}
原文地址:https://www.cnblogs.com/zzPrince/p/5660513.html