Async Return Types
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.
- The Result property is a blocking property. If
- 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的更多相关文章
- Async Task Types in C#
Async Task Types in C# Extend async to support task types that match a specific pattern, in addition ...
- Trailing return types
Trailing return types是C++11关于函数声明的语言特性之一,旨在解决模版编程遇到的语法相关的问题,先看一个简单例子,感受一下什么是trailing return types: C ...
- The return types for the following stored procedures could not be detected
1.使用dbml映射数据库,添加存储过程到dbml文件时报错. 2.原因:存储过程中使用了临时表 3.解决方案 3.1 通过自定义表值变量实现 Ex: DECLARE @TempTable TABLE ...
- 编程概念--使用async和await的异步编程
Asynchronous Programming with Async and Await You can avoid performance bottlenecks and enhance the ...
- 转 Using Async for File Access
原文:https://msdn.microsoft.com/en-us/library/jj155757.aspx using System; using System.Collections.Gen ...
- 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 ...
- look look C#7
vs2017也rc好几个版本了,本想跟进看看c#7加入了什么内容,去搜索c#7,确实找到了不少文章,无奈很多特性ide根本不让编译啊...所以今天主要列出已经确定了的c#7特性(一般来说rc后也不会加 ...
- [干货来袭]C#7.0新特性(VS2017可用)
前言 微软昨天发布了新的VS 2017 ..随之而来的还有很多很多东西... .NET新版本 ASP.NET新版本...等等..太多..实在没消化.. 分享一下其实2016年12月就已经公布了的C#7 ...
- C#7的9个新语法
一.out变量 在c#7之前我们得这样 在c#7中我们可以这样 当然你还可以使用"var" 这算一个小更新,其实这个问题存在很久了,应该也很好解决,不知为何到c#7才开始引入,不管 ...
随机推荐
- 如何将你拍摄的照片转换成全景图及六面体(PTGui)
在完成全景照片的拍摄之后,接下来,我们需要的是进行全景图的拼接.全景图片分为两种类型1.立方体全景图(6面体)制作全景时通常使用该种格式 如下图 2.球形图(2:1的单张全景图片)2:1全景图宽高比例 ...
- 长时间没有操作putty就会断开连接是怎么回事?
seconds between keepalives 设置为10就好了, 这个值有什么含义,服务器为了节省资源采取了一些措施,其中一条就是如果检测一个会话(session)几分钟或者几小时没有数据流入 ...
- PAT 甲级1001 A+B Format (20)(C++ -思路)
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- Startup.国外新锐公司及其技术Blog
国外技术公司Tech/Engineering Blog 1. vimeo https://coderwall.com/team/vimeo http://blog.assembly.com/ 2. l ...
- Centos安装配置Postfix邮件服务器--网址
http://www.haiyun.me/archives/centos-install-postfix.html http://blog.csdn.net/liuyunfengheda/articl ...
- 学习python 多进程和多线程
''' 学习多进程和多线程 ''' import multiprocessing def deadLoop(): while True: pass if __name__ == '__main__': ...
- android DatagramSocket send 发送数据出错
安卓4.0以后好像不能在主线程里面使用 socket 所以不管是发送数据还是接收数据需要新开一个了线程: 以下代码是我点击发送是代码: new Thread(new Runnable() { @Ove ...
- Vue 汇总
1.右键菜单(防止默认行为) @contextmenu.native.prevent="rightClick()"
- OneZero_Aphla发布总结以及自己的体会
Aphla发布正式结束了.清明时节,总要祭奠点什么. 以下是这一周的燃尽图. 可以发现,并没有燃尽.所以OneZero的Aphla发布失败了. 失败原因有至少以下三点: 1.组长分配任务存在隐患,高风 ...
- jvm层面锁优化+一般锁的优化策略
偏向锁: 首先了解对象头MARK指针(对象头标记,32位): 存储GC标记,对象年龄,对象Hash,锁信息(锁记录的指针,偏向锁线程的ID) 大部分情况是没有竞争的,所以可以通过偏向来提高性能 所谓的 ...