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 (传统意义上的 ... 
随机推荐
- grep常见使用方法总结
			grep -E 'l\{2,\}' 2.txt grep -E 'h(ell|a)o' test.txt grep '[a-z]\{5,\}' test.txt grep -xf a.txt b.tx ... 
- ZOJ 3494  BCD Code  (AC自己主动机 + 数位DP)
			题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ... 
- hive 运行sqlclient异常
			FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. java.lang.Runtim ... 
- hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】
			Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ... 
- windows上通过vnc连接虚拟机中linux系统
			首先要在虚拟机中安装vnc. 虚拟机的设置中要启用VNC连接. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHdzc2c=/font/5a6L5L2T/ ... 
- UVA - 10229  Modular Fibonacci  矩阵快速幂
			Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ... 
- select into in mysql
			http://stackoverflow.com/questions/16809393/select-into-in-mysql Use the CREATE TABLE SELECT syntax. ... 
- nyoj--325--zb的生日(简单dp)
			zb的生日 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么庆祝 ... 
- POJ 2189 枚举
			题意: n头牛,p长度的道路,问至多包括c头牛的道路最长有多长. 思路: 按照题意暴力就好-- 注意边界. // by SiriusRen #include <cstdio> #inclu ... 
- SpringBoot(五)  番外---Docker
			Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Li ... 
