Async methods have three possible return types: Task<TResult>Task,
and void.

  • The Task<TResult> return type is used for an async method that contains a return (C#)
    statement in which the operand has type TResult.

    • The Result property is a blocking property. If
      you try to access it before its task is finished, the thread that's currently active is blocked until the task completes and the value is available. In most cases, you should access the value by using await instead
      of accessing the property directly.
  • Async methods that don't contain a return statement or that contain a return statement that doesn't return an operand usually have a return type of Task. Such
    methods would be void-returning methods if they were written to run synchronously. If you use a Task return type for an async method, a calling method can use an await operator
    to suspend the caller's completion until the called async method has finished.
  • The primary use of the void return type is in event handlers, where a void return type is required. A
    void return also can be used to override void-returning methods or for methods that perform activities that can be categorized as "fire and forget." However, you should return a Task wherever possible, because a void-returning
    async method can't be awaited. Any caller of such a method must be able to continue to completion without waiting for the called async method to finish, and the caller must be independent of any values or exceptions
    that the async method generates.

    The caller of a void-returning async method can't catch exceptions that are thrown from the method, and
    such unhandled exceptions are likely to cause your application to fail. If an exception occurs in an async method that returns a Task or Task<TResult>,
    the exception is stored in the returned task, and rethrown when the task is awaited. Therefore, make sure that any async method that can produce an exception has a return type of Task or Task<TResult> and
    that calls to the method are awaited.

Async Return Types的更多相关文章

  1. Async Task Types in C#

    Async Task Types in C# Extend async to support task types that match a specific pattern, in addition ...

  2. Trailing return types

    Trailing return types是C++11关于函数声明的语言特性之一,旨在解决模版编程遇到的语法相关的问题,先看一个简单例子,感受一下什么是trailing return types: C ...

  3. The return types for the following stored procedures could not be detected

    1.使用dbml映射数据库,添加存储过程到dbml文件时报错. 2.原因:存储过程中使用了临时表 3.解决方案 3.1 通过自定义表值变量实现 Ex: DECLARE @TempTable TABLE ...

  4. 编程概念--使用async和await的异步编程

    Asynchronous Programming with Async and Await You can avoid performance bottlenecks and enhance the ...

  5. 转 Using Async for File Access

    原文:https://msdn.microsoft.com/en-us/library/jj155757.aspx using System; using System.Collections.Gen ...

  6. C#的多线程——使用async和await来完成异步编程(Asynchronous Programming with async and await)

    https://msdn.microsoft.com/zh-cn/library/mt674882.aspx 侵删 更新于:2015年6月20日 欲获得最新的Visual Studio 2017 RC ...

  7. look look C#7

    vs2017也rc好几个版本了,本想跟进看看c#7加入了什么内容,去搜索c#7,确实找到了不少文章,无奈很多特性ide根本不让编译啊...所以今天主要列出已经确定了的c#7特性(一般来说rc后也不会加 ...

  8. [干货来袭]C#7.0新特性(VS2017可用)

    前言 微软昨天发布了新的VS 2017 ..随之而来的还有很多很多东西... .NET新版本 ASP.NET新版本...等等..太多..实在没消化.. 分享一下其实2016年12月就已经公布了的C#7 ...

  9. C#7的9个新语法

    一.out变量 在c#7之前我们得这样 在c#7中我们可以这样 当然你还可以使用"var" 这算一个小更新,其实这个问题存在很久了,应该也很好解决,不知为何到c#7才开始引入,不管 ...

随机推荐

  1. [z]protobuf实现c++与java之间的数据传递,beancopy数据到前台

    [z]http://blog.csdn.net/xhyzdai/article/details/46684335 定义proto文件 option java_package = "com.w ...

  2. OpenGL3D图形、旋转、纹理、键盘移动、光照、滤波、透明(完整) 转自http://www.cnblogs.com/tiandsp/archive/2012/01/23/2329049.html

    #include <windows.h> // Windows的头文件 #include <stdio.h> #include <gl\gl.h> // OpenG ...

  3. How to install VCM 2 Ford IDS 109 software

    How to install Ford IDS 109: 1- Install the ids 86 before changing the date to 1 07 2015 (hold the d ...

  4. jsonp,ajax,json问题

    JSONP技术 JSONP是解决跨域问题的一种常见方式 跨域问题,因为浏览器有同源策略,所以当不同域间进行数据交互的时候就会出现跨域问题 同源策略:只有在同协议.同域名.同端口的情况下才能进去数据交互 ...

  5. MySQL学习笔记-cache 与 buffer

    Cache和Buffer是两个不同的概念,简单的说,Cache是加速"读",而 buffer是缓冲"写",前者解决读的问题,保存从磁盘上读出的数据,后者是解决写 ...

  6. C++11与Unicode及使用标准库进行UTF-8、UTF-16、UCS2、UCS4/UTF-32编码转换

    zt https://blog.poxiao.me/p/unicode-character-encoding-conversion-in-cpp11/ Unicode Unicode是计算机领域的一项 ...

  7. pc-H5 适配方案

    一.介绍 在前端项目页面开发中,尤其是H5页面开发,我们常常要适配各种分辨率的屏幕,才能让用户获得最好的体验效果.pc也是如此,很多页面是一屏,也是要适配各种尺寸的分辨率.这时候我们就需要对各种分辨率 ...

  8. centos 6.5 搭建zookeeper集群

    为什么使用Zookeeper? 大部分分布式应用需要一个主控.协调器或控制器来管理物理分布的子进程(如资源.任务分配等)目前,大部分应用需要开发私有的协调程序,缺乏一个通用的机制协调程序的反复编写浪费 ...

  9. oracle 提示没有监听

    # listener.ora Network Configuration File: E:\Oracle10g\network\admin\listener.ora # Generated by Or ...

  10. connect: Address is invalid on local machine or port is not valid on remote

    idea 运行正常打成jar包运行提示“connect: Address is invalid on local machine or port is not valid on remote” , 解 ...