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. linux内核(三)文件系统

    1.为什么需要根文件系统 (1)init进程的应用程序在根文件系统上(2)根文件系统提供了根目录/(3)内核启动后的应用层配置(etc目录)在根文件系统上.几乎可以认为:发行版=内核+rootfs(4 ...

  2. 解析如何利用ElasticSearch和Redis检索和存储十亿信息

    如果从企业应用的生存率来看,选择企业团队信息作为主要业务,HipChat的起点绝非主流:但是如果从赚钱的角度上看,企业市场的高收益确实值得任何公司追逐,这也正是像JIRA和Confluence这样的智 ...

  3. Eclipse删除多余工作空间

    选择perferences-->General -->Startup and Shutdown-->workspace-->选择多余的工作空间 -> remove -&g ...

  4. HDU 4165

    一块药看成括号配对就行了.很明显的直接求卡特兰数. 今晚看了HDU 3240的题,有一点思路,但无情的TLE.想不到什么好方法了,看了别人的解答,哇...简直是天才的做法啊....留到星期六自己思考一 ...

  5. 【转载】黑客内核:编写属于你的第一个Linux内核模块

    黑客内核:编写属于你的第一个Linux内核模块

  6. 面向程序猿的设计模式 ——GoF《设计模式》读书总结(壹)抽象工厂&amp;生成器

    第一部分:创建型模式 创建型模式抽象了实例化过程. 它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.(把一些小的对象组装成大对象,这个工作由专门的类对象来做) 一个类创建型模式使用继承改变被实 ...

  7. bzoj2748: [HAOI2012]音量调节(背包)

    2748: [HAOI2012]音量调节 题目:传送门 题解: sb省选题..呵呵一眼背包: f[i][j]表示第i时刻能否为音量j 代码: #include<cstdio> #inclu ...

  8. JAVA设计模式之【迭代器模式】

    迭代器模式 聚合类,聚合对象 电视机遥控器,迭代器,遍历局和对象中的成员 灵活新增遍历方法 将负责遍历数据的方法提取出来,封装到专门的类中,实现数据存储和数据遍历分离.这就是迭代器模式. 灵活性,可扩 ...

  9. Python 异常(Exception)

    1. 字符串为构造函数的参数 >> raise Exception('hyperdirve overload') Exception Traceback (most recent call ...

  10. Oracle RAC --安装图解

    规划:所用linux系统以虚拟化方式安装在esx上,配置有内网的yum源,各分配有16G存储,下面为简单拓扑图 一,下载软件1,地址:http://www.oracle.com/technology/ ...