什么是返回void和返回任务之间的区别? [英] What's the difference between returning void and returning a Task?

查看:477
本文介绍了什么是返回void和返回任务之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找不同的C#异步CTP样我看到一些异步函数返回无效,和其他人返回非通用任务。我明白为什么返回一个任务< MyType的> 是非常有用的异步操作完成时将数据返回给调用者,但我见过的函数有返回类型的工作永远不会返回任何数据。为什么不返回无效

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?

推荐答案

SLaks和Killercam的回答都不错;我想我只需要添加更多的内容。

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方法可以返回无效工作任务&LT; T&GT; 。它们之间有什么区别呢?

A method marked as async can return void, Task or Task<T>. What are the differences between them?

A 任务&LT; T&GT; 返回异步方法可以等待,并在任务完成时,它会毫无顾忌了一个T.

A Task<T> returning async method can be awaited, and when the task completes it will proffer up a T.

A 任务返回异步方法可以期待,任务完成时,任务的延续计划运行。

A Task returning async method can be awaited, and when the task completes, the continuation of the task is scheduled to run.

A 无效返回异步方法不能等待;它是一种发射后不管的方法。它异步工作,你有没有当它完成办法告诉。这是不是有点不可思议以上;作为SLaks说,通常你只会进行异步事件处理程序,做到这一点。该事件触发时,处理程序执行;没有人会为等待的事件处理程序返回,因为事件处理程序不返回任务的任务,即使他们做到了,什么code将使用任务的东西?它通常不是用户code将控制转移给摆在首位的处理器。

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.

您的第二个问题,在一个评论,本质上是关于什么都可以的await 编辑:

Your second question, in a comment, is essentially about what can be awaited:

什么样的​​方法可以是的await ED?一个空隙返回方法可以的await ED?

What kinds of methods can be awaited? Can a void-returning method be awaited?

没有,一个空洞的回归方法,不能等待。编译器把等待M()成调用 M()。GetAwaiter(),其中 GetAwaiter 可能是一个实例方法或扩展方法。期待已久的值必须一个,你可以得到一个awaiter;显然空隙返回方法不产生价值,从中可以得到一个awaiter。

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.

任务 -returning方法可以产生awaitable值。我们预计,第三方将需要创建自己的工作的实施样可以期待已久的对象,你就可以在等待他们。然而,你将不会被允许申报返回任何东西,但无效工作异步方法任务&LT; T&GT;

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>.

这篇关于什么是返回void和返回任务之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆