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. nutch+hadoop 配置使用

    nutch+hadoop 配置使用 配置nutch+hadoop 1,下载nutch.如果不需要特别开发hadoop,则不需要下载hadoop.因为nutch里面带了hadoop core包以及相关配 ...

  2. java io包File类

    1.java io包File类, Java.io.File(File用于管理文件或目录: 所属套件:java.io)1)File对象,你只需在代码层次创建File对象,而不必关心计算机上真正是否存在对 ...

  3. Qt之命令行参数

    简述 在Qt之进程间通信(QProcess)一节,我们讲解了如何通过QProcess来进行进程间的通信.主要通过启动外部程序,然后通过命令行的方式传递参数. 这里,我们可以通过Qt Creator来设 ...

  4. 自己定义控件-MultipleTextView(自己主动换行、自己主动补齐宽度的排列多个TextView)

    一.功能: 1.传入一个 List<String> 数组,控件会自己主动加入TextView,一行显示不下会自己主动换行.而且把上一行末尾的空白通过拉伸而铺满. 2.配置灵活 <co ...

  5. HDU 4372

    想了很久,终于想到了.... 向后看到F,向前看到B,假如把N-1个楼分成F+B个组,则把每个组最高的楼作为看到的楼,那么,其实在确定每一组的最高楼时,左边或右边的最高楼的顺序已经确定了.由于是排列数 ...

  6. YAML说明

    YAML说明 https://www.cnblogs.com/songchaoke/p/3376323.html XML的简化

  7. angular4过滤器

    Angular4中过滤器 一.大小写转换过滤器 uppercase将字符串转换为大写 lowercase将字符串转换为小写 <p>将字符串转换为大写{{str | uppercase}}& ...

  8. ASP页面的执行顺序

    http://hi.baidu.com/yanjiezhu/item/29c113c3912e2a0ac710b2d3 1.对象初始化(OnInit方法) 页面中的控件(包括页面本身)都是在它们最初的 ...

  9. python的一些配置

    昨天西邮的学友让我看一段python svm的输入文件格式,但是我打开很久不用的pycharm后发觉python包早已过时.于是搜了一下教程,看来python也得同时补习了 另外,机器学习还需要装很多 ...

  10. MySQL学习(六)——自定义连接池

    1.连接池概念 用池来管理Connection,这样可以重复使用Connection.有了池,我们就不用自己来创建Connection,而是通过池来获取Connection对象.当使用完Connect ...