Async Return Types
来源:互联网 发布:excel数据分析 编辑:程序博客网 时间:2024/06/01 21:59
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
- Return types of virtual functions
- javascript async promise return 测试
- Tuple Types and Multiple Return Values
- iOS keyboard的types return变成什么样
- 6.3Return Types and the return Statement &6.5Overloaded functions
- Item 31. Covariant Return Types & Item 32. Preventing Copying
- new types may not define a return type - C++
- new types may not be defined in a return type
- types
- Async
- async
- Async
- async
- async
- async
- async
- @async
- 第17周项目13-比较两个字符串
- C++的学习(转)
- 使用OpenSSL发送IOS推送通知 Apple Push Notification
- CSS 背景
- Java HashMap的死循环
- Async Return Types
- MQTT的学习研究(四)moquette-mqtt 的使用之mqtt Blocking API客户端订阅并接收主题信息
- [Linux] 账户管理
- java
- 【iOS开发必备指南合集】申请企业级IDP、真机调试、游戏接入GameCenter 指南(实现仿官方的成就提示框)、游戏接入OpenFeint指南
- const用法详解(转)
- Kernel Debugging Using Kprobe and Jprobe
- MQTT的学习研究(五) MQTT moquette 的 Blocking API 发布消息服务端使用
- CF Div211 (C)