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. Sublime Text 3 Package Control安装

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50618314 安装好Sublime T ...

  2. 【Linux】进程调度概述

    1 可运行队列 (基于实时进程调度) 调度程序中最主要的数据结构式运行队列(runqueue).可运行队列是给定处理器上的可运行进程的链表,每一个处理器一个. 每一个可投入运行的进程都唯一的归属于一个 ...

  3. python学习之--安装IDE(eclipse+pydev)

    First steps download eclipse url:http://www.eclipse.org/downloads/ select Help -> Install New Sof ...

  4. 黑马day15 文件上传&amp;apche的工具包

    1.肯定要导入apche的jar包 2.要使用的类的介绍.. 2.1DiskFileItemFactory  public DiskFileItemFactory(int sizeThreshold, ...

  5. node08---Express框架

    一.Express框架 Express框架是后台的Node框架,所以和jQuery.zepto.yui(雅虎的).bootstrap都不一个东西. Express在后台的受欢迎的程度,和jQuery一 ...

  6. iOS定义静态变量、静态常量、全局变量

    静态变量 当我们希望一个变量的作用域不仅仅是作用域某个类的某个对象,而是作用域整个类的时候,这时候就可以使用静态变量. staticstatic修饰的变量,是一个私有的全局变量.C或者Java中sta ...

  7. MYSQL binlog 日志内容查看

    记录mysql数据库真正执行更改的所有操作(DML语句),不包含那些没有修改任何数据的语句,不会记录select和show这样的语句. 二进制日志的作用: 1. 可以完成主从复制的功能 2. 进行恢复 ...

  8. COGS 2479 奇怪的姿势卡♂过去 (bitset+折半)

    思路: 此题显然是CDQ套CDQ套树套树 (然而我懒) 想用一种奇怪的姿势卡过去 就出现了以下解法 5w*5w/8的bitset hiahiahia 但是空间会爆怎么办啊- 折半~ 变成5w*2.5w ...

  9. 页面的URL分析----window.location.href

    window.location是页面的位置对象window.location.href是 location的一个属性值,并且它是location的默认属性. window.location直接赋值一个 ...

  10. C++提高速度