返回 void 和返回 Task 有什么区别? [英] What's the difference between returning void and returning a Task?

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

问题描述

在查看各种 C# 异步 CTP 示例时,我看到一些异步函数返回 void,而其他一些返回非通用 Task.我可以理解为什么在异步操作完成时返回 Task 对将数据返回给调用者很有用,但是我看到的函数具有 Task<的返回类型/code> 从不返回任何数据.为什么不返回void?

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.

您的第一个问题本质上是关于哪些方法可以标记为async.

Your first question is essentially about what methods can be marked async.

标记为 async 的方法可以返回 voidTaskTask.它们之间有什么区别?

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

可以等待一个 Task 返回异步方法,当任务完成时它会提供一个 T.

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

可以等待返回异步方法的Task,当任务完成时,将安排任务的继续运行.

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

不能等待 void 返回异步方法;这是一种即发即忘"的方法.它确实是异步工作的,你无法知道它何时完成.这有点奇怪;正如 SLaks 所说,通常你只会在制作异步事件处理程序时这样做.事件触发,处理程序执行;没有人会等待"事件处理程序返回的任务,因为事件处理程序不返回任务,即使他们这样做了,什么代码会使用任务来做某事?通常首先将控制权转移给处理程序的不是用户代码.

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.

你的第二个问题,在评论中,本质上是关于什么可以awaited:

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

可以await的方法有哪些?返回 void 的方法可以awaited吗?

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

不,不能等待返回空值的方法.编译器将 await M() 翻译成对 M().GetAwaiter() 的调用,其中 GetAwaiter 可能是一个实例方法或扩展方法.等待的值必须是您可以获得等待者的值;显然,返回 void 的方法不会产生可以从中获取等待者的值.

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 返回方法可以产生可等待的值.我们预计第三方会希望创建他们自己的类似 Task 的可等待对象的实现,并且您将能够等待它们.但是,您将不被允许声明返回除 voidTaskTaskasync 方法>.

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

(更新:我的最后一句话可能被未来版本的 C# 篡改;有一项提议允许异步方法的返回类型不是任务类型.)

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

(更新:上面提到的特性使其成为 C# 7.)

(UPDATE: The feature mentioned above made it in to C# 7.)

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

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