zoukankan      html  css  js  c++  java
  • What's the difference between returning void and returning a Task?

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

  • 相关阅读:
    k8s keepalived haproxy 集群成功
    .Net Core 用 EntityFramework 读取 Oracle
    Fedora CoreOS 安装 非LInux专业国内第一手
    Docker 下的 Keepalived + Haproxy 高可用实现 1 实现结果演示
    第二篇 windows container 微软的原生容器
    第一篇 Windows docker 概述
    测试openLiveWrite写博客
    比特币勒索病毒肆虐,腾讯云安全专家给你支招
    WannaCry 勒索病毒用户处置指南
    pytorch 学习笔记之编写 C 扩展,又涨姿势了
  • 原文地址:https://www.cnblogs.com/chucklu/p/6482855.html
Copyright © 2011-2022 走看看