What's 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
asynccan returnvoid,TaskorTask<T>. What are the differences between them?
A Task<T> returning async method can be awaited, and when the task completes it will proffer up a T.
A Task returning async method can be awaited, and when the task completes, the continuation of the task is scheduled to run.
A 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 beawaited?
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 void, Task 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?的更多相关文章
- signal函数:void (*signal(int,void(*)(int)))(int);
http://blog.chinaunix.net/uid-20178794-id-1972862.html signal函数:void (*signal(int,void(*)(int)))(int ...
- DAX:New and returning customers
The New and Returning Customers pattern dynamically calculates the number of customers with certain ...
- typedef void (*funcptr)(void) typedef void (*PFV)(); typedef int32_t (*PFI)();
看到以下代码,不明白查了一下: /** Pointer to Function returning Void (any number of parameters) */ typedef void (* ...
- 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 ...
- 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 ...
- Supported method argument types Spring MVC
Supported method argument types The following are the supported method arguments: Request or respons ...
- 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 ...
- Threading in C# 5
Part 5: Parallel Programming In this section, we cover the multithreading APIs new to Framework 4.0 ...
- C++ lvalue,prvalue,xvalue,glvalue和rvalue详解(from cppreference)
General 每一个C++表达式(一个操作符和它的操作数,一个字面值,一个变量名等等)都代表着两个独立属性:类型+属性分类.在现代C++中 glvalue(泛左值) = lvalue (传统意义上的 ...
随机推荐
- 葡萄城公布新版ActiveReports 9报表控件和报表server
2014年11月10日---葡萄城宣布正式公布ActiveReports9,包含了三种报表模型:RDL报表.页面报表.区域报表.对于ActiveReports中的这个最新版本号中,我们专注于提高产品的 ...
- 2.windows下安装git
转自:https://blog.csdn.net/lvkelly/article/details/54666868
- SSRS故障排除
1.SSRS部署到本地出现错误:为用户“Jimmy-PC\Jimmy”授予的权限不足,无法执行此操作.用户“Jimmy-PC\Jimmy”不具有所需的权限.请验证授予了足够的权限并且解决了 Windo ...
- javascript常用代码(不完整版)
求大神指点 Javascript嵌入式 <script typt:javascript>代码</script> 注释 //或者/*内容*/ 变量名赋值 Var 变量名 = 值 ...
- codeforces 493 D Vasya and Chess【 博弈 】
题意:给出n*n的棋盘,白方在(1,1),黑方在(1,n)处,每一步可以上下左右对角线走,哪个先抓到另一个,则它获胜 可以画一下,发现n是奇数的时候,白方先走,无论它怎么走,黑方和它走对称的,黑方都一 ...
- POJ 3273 Monthly Expense 【二分答案】
题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...
- ZOJ 3321 Circle【并查集】
解题思路:给定n个点,m条边,判断是否构成一个环 注意到构成一个环,所有点的度数为2,即一个点只有两条边与之相连,再有就是判断合并之后这n个点是否在同一个连通块 Circle Time Limit: ...
- Synchronization (computer science)
过程同步.数据同步. In computer science, synchronization refers to one of two distinct but related concepts: ...
- 小型ceph集群的搭建
了解ceph DFS(distributed file system)分布式存储系统,指文件系统管理的物理存储资源,不一定直接连接在本地节点上,而是通过计算机网络与节点相连,众多类别中,ceph是当下 ...
- Mathab和Python的numpy中的数组维度
Matlab和Python的numpy在维度索引方面的不同点: 1.索引的起始点不同:Matlab起始位置的索引为1,Python为0. 2.索引的括号不同:Matlab中元素可以通过小括号表示索引, ...