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. mysql命令行导入结构化数据

    数据样本 103252765-|--|-stephanie_mt@hotmail.com-|-o/35+nGaNEU=-|-ion|-- 其中|为分隔符,每行的换行符\n mysql -uroot M ...

  2. webstorm打开一个门户工程流程

    1.电脑上安装了nginx   ,进入conf目录下,找到nginx.conf打开 2.将原配置中server部分替换掉 server{ listen       80;     server_nam ...

  3. mySLQ数据库 练习题

    MySQL 练习题1 DROP TABLE IF EXISTS `liuyan`; CREATE TABLE `liuyan` ( `id` int(11) NOT NULL AUTO_INCREME ...

  4. 免费证书https://lamp.sh/ssl.html

    https(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的 http 通道,简单讲是 http 的安全版.即 ht ...

  5. Jmeter常用脚本开发之FTP请求

    1.没有FTP站点的,可以自己搭建一个FTP站点供测试使用,搭建步骤: l  安装IIS组件,控制面板—>程序和功能—>启用或关闭windows功能,勾选FTP服务器.IIS管理控制台,点 ...

  6. 常用到的photoshop实用设计功能都在这了!

    常用到的photoshop实用设计功能都在这了!赶快收藏学起来,需转不谢~ ​ 编辑:千锋UI设计

  7. Window7安装tensorflow整套环境详细流程

    安装tensorflow方式有好多种,为了方便编译环境以及包管理,这里采用Anaconda平台安装tensorflow. tensorflow官网:http://www.tensorflow.org/ ...

  8. 域名ping不通,ip地址ping得通

    原因:dns服务器过期,需要更换dns服务器地址

  9. Creating Your Own PHP Helper Functions In Laravel

    By Hamza Ali LAST UPDATED AUG 26, 2018  12,669 104 Laravel provides us with many built-in helper fun ...

  10. jquery动态出操作select

    var citys = {1:'北京',2:'上海',3:'广州',4:'深圳'}; $("#city option:gt(0)").remove(); for(var k in ...