.net - 什么值得期待?

标签 .net asynchronous async-await

我听说 awaitable 是一种异步操作。但是既然是一个重要的概念,为什么我在 MSDN 上找不到准确的定义呢?

我的问题不是如何编写 async/await。我的问题是了解这个概念。 MSDN有概念async/await但没有 awaitable .

那么什么是可等待的呢?如果是手术,包括什么?

最佳答案

最后我在 Async/Await FAQ. 上找到了它

An “awaitable” is any type that exposes a GetAwaiter method which returns a valid “awaiter”. This GetAwaiter method may be an instance method (as it is in the case of Task and Task<TResult>), or it may be an extension method.

An “awaiter” is any type returned from an awaitable’s GetAwaiter method and that conforms to a particular pattern. The awaiter must implement the System.Runtime.CompilerServices.INotifyCompletion interface, and optionally may implement the System.Runtime.CompilerServices.ICriticalNotifyCompletion interface. In addition to providing an implementation of the OnCompleted method that comes from INotifyCompletion (and optionally the UnsafeOnCompleted method that comes from ICriticalNotifyCompletion), an awaiter must also provide an IsCompleted Boolean property, as well as a parameterless GetResult method. GetResult returns void if the awaitable represents a void-returning operation, or it returns a TResult if the awaitable represents a TResult-returning operation.


还有这个 link about awaitable and awaiter is helpful.

关于.net - 什么值得期待?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40853158/

相关文章:

c# - 使用 ToList 的异步、等待和任务

javascript - async/await 返回 Promise { <pending> }

c# - Visual Studio Xamarin 模板缺少程序集引用

.net - URL 编码 URL

c# - 同步调用异步嵌套方法 - 异步会在路径上起作用吗?

ios - dispatch_group_notify 不等待一个 dispatch_group_async

javascript - 为什么 async/await 会产生不同的结果

c# - 我是作为服务运行的吗

c# - 找不到参数引用 (%1) 的描述字符串

iphone - 如何告诉 iPhone NSURLConnection 中止