http://stackoverflow.com/questions/8043296/whats-the-difference-between-returning-void-and-returning-a-task

问题:

In looking at various C# Async CTP samples I see some async functions that return void, and others that return the non-generic Task.

I can see why returning a Task<MyType> is useful to return data to the caller when the async operation completes, but the functions that I've seen that have a return type of Task never return any data.

Why not return void?

如果返回值是Task的时候,在await的时候,Resharper提示:Awaited Task returns no value.

回答:

SLaks and Killercam's answers are good; I thought I'd just add a bit more context.

Your first question is essentially about what methods can be marked async.

A method marked as async can return voidTask or Task<T>. What are the differences between them?

Task<T> returning async method can be awaited, and when the task completes it will proffer up a T.

Task returning async method can be awaited, and when the task completes, the continuation of the task is scheduled to run.

void returning async method cannot be awaited; it is a "fire and forget" method.

It does work asynchronously, and you have no way of telling when it is done.

This is more than a little bit weird; as SLaks says, normally you would only do that when making an asynchronous event handler.

The event fires, the handler executes; no one is going to "await" the task returned by the event handler because event handlers do not return tasks, and even if they did, what code would use the Task for something?

It's usually not user code that transfers control to the handler in the first place.

Your second question, in a comment, is essentially about what can be awaited:

What kinds of methods can be awaited? Can a void-returning method be awaited?

No, a void-returning method cannot be awaited.

The compiler translates await M() into a call to M().GetAwaiter(), where GetAwaiter might be an instance method or an extension method.

The value awaited has to be one for which you can get an awaiter; clearly a void-returning method does not produce a value from which you can get an awaiter.

Task-returning methods can produce awaitable values.

We anticipate that third parties will want to create their own implementations of Task-like objects that can be awaited, and you will be able to await them.

However, you will not be allowed to declare async methods that return anything but voidTask or Task<T>.

(UPDATE: My last sentence there may be falsified by a future version of C#; there is a proposal to allow return types other than task types for async methods.)

What's the difference between returning void and returning a Task?的更多相关文章

  1. signal函数:void (*signal(int,void(*)(int)))(int);

    http://blog.chinaunix.net/uid-20178794-id-1972862.html signal函数:void (*signal(int,void(*)(int)))(int ...

  2. DAX:New and returning customers

    The New and Returning Customers pattern dynamically calculates the number of customers with certain ...

  3. typedef void (*funcptr)(void) typedef void (*PFV)(); typedef int32_t (*PFI)();

    看到以下代码,不明白查了一下: /** Pointer to Function returning Void (any number of parameters) */ typedef void (* ...

  4. Async/Await - Best Practices in Asynchronous Programming z

    These days there’s a wealth of information about the new async and await support in the Microsoft .N ...

  5. Chapter 4: Spring and AOP:Spring's AOP Framework -- draft

    Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...

  6. Supported method argument types Spring MVC

    Supported method argument types The following are the supported method arguments: Request or respons ...

  7. Managing DbContext the right way with Entity Framework 6: an in-depth guide by mehdime

    UPDATE: the source code for DbContextScope is now available on GitHub: DbContextScope on GitHub. A b ...

  8. Threading in C# 5

    Part 5: Parallel Programming In this section, we cover the multithreading APIs new to Framework 4.0 ...

  9. C++ lvalue,prvalue,xvalue,glvalue和rvalue详解(from cppreference)

    General 每一个C++表达式(一个操作符和它的操作数,一个字面值,一个变量名等等)都代表着两个独立属性:类型+属性分类.在现代C++中 glvalue(泛左值) = lvalue (传统意义上的 ...

随机推荐

  1. 小胖说事30------iOS 强制转成横屏的方式

    一直遇到这个问题,今天最终找到了解决方法. 在我们的项目中常常遇到横竖屏切换,而又有某个特定的界面必须是特定的显示方式(横屏或竖屏).这就须要例如以下的处理了. 强制转成横屏: if ([[UIDev ...

  2. linux数据库升级

    转自:老左博客:http://www.laozuo.org/6145.html 老左今天有在帮朋友的博客搬迁到另外一台VPS主机环境,其环境采用的是LLSMP架构的,原先的服务器采用的是LNMP网站环 ...

  3. bzoj5157: [Tjoi2014]上升子序列(树状数组LIS)

    5157: [Tjoi2014]上升子序列 题目:传送门 题解: 学一下nlogn的树状数组求最长上生子序列就ok(%爆大佬) 离散化之后,用一个数组记录一下,直接树状数组做 吐槽:妈耶...一开始不 ...

  4. Mysql实战45讲 04讲深入浅出索引(上)读书笔记 极客时间

    极客时间 Mysql实战45讲 04讲深入浅出索引 极客时间(上)读书笔记  笔记体悟 1.索引的作用:提高数据查询效率2.常见索引模型:哈希表.有序数组.搜索树3.哈希表:键 - 值(key - v ...

  5. 137.CPP自带异常

    #include <iostream> #include <exception> using namespace std; //继承自带的异常 class sizeerror ...

  6. iframe 高度宽度自适应

    <iframe id="iframeHome" name="iframeHome" src="/Page/NewHome/GongZuoTai. ...

  7. MySQL学习(四)——外键

    1.比方现在有两张表“分类表”和“商品表”,为了表明商品属于哪个分类,通常我们将在商品表上添加一列,用于存放分类cid的信息,此列称为:外键. 此时分类表category称为主表,cid称为主键:商品 ...

  8. c#学习0217

    1 继承 继承 1 子类是否继承了父类的构造函数 答案:子类并没有继承父类的构造函数 但是子类或默认调用父类的无参数的构造函数 在子类中创建父类对象 这样子类才可以使用父类的成员 如果在父类中声明了有 ...

  9. codeforces 493 C Vasya and Basketball

    题意:给出三分线的值d,分别有两支队伍,如果小于等于d,得2分,如果大于d,得三分,问使得a-b最大时的a,b 一看到题目,就想当然的去二分了----啥都没分出来---55555555 后来才知道不能 ...

  10. 我的Java历程_maven配置的心路历程

    从github上download了个maven管理的开源项目,接下来随笔下安装maven的心路历程: 异常尴尬的是import进ide之后一个红色的感叹号!震惊!google一下知道了,maven没配 ...