虚拟人的自定义等待 [英] Custom awaitables for dummies

查看:26
本文介绍了虚拟人的自定义等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

异步/等待常见问题解答中,斯蒂芬·图布 说:

In Async/Await FAQ, Stephen Toub says:

awaitable 是任何公开 GetAwaiter 方法的类型,该方法返回一个有效的 awaiter.
...
awaiter 是从 awaitableGetAwaiter 方法返回的任何类型,并且符合特定模式.

An awaitable is any type that exposes a GetAwaiter method which returns a valid awaiter.
...
An awaiter is any type returned from an awaitable’s GetAwaiter method and that conforms to a particular pattern.

所以为了成为等待者,类型应该:

So in order to be an awaiter, a type should:

  • 实施 INotifyCompletion 界面.
  • 提供一个名为 IsCompleted 的布尔属性.
  • 提供一个无参数的 GetResult 方法,返回 voidTResult.
  • Implement the INotifyCompletion interface.
  • Provide a boolean property called IsCompleted.
  • Provide a parameterless GetResult method that returns void or TResult.

(我暂时忽略了 ICriticalNotifyCompletion.)

我知道我提到的页面有一个示例,展示了编译器如何翻译 await 操作,但我仍然很难理解.

I know the page I mentioned has a sample that shows how the compiler translates await operations but I'm stil having a hard time understanding.

当我等待一个等待时,

  • 什么时候检查IsCompleted?我应该在哪里设置它?
  • 什么时候调用 OnCompleted?
  • 哪个线程调用OnCompleted?
  • 我在不同的示例中看到了直接调用 OnCompleted 的 continuation 参数和使用 Task.Run(continuation) 的示例,我应该选择哪个,为什么?
  • When is IsCompleted checked? Where should I set it?
  • When is OnCompleted called?
  • Which thread calls OnCompleted?
  • I saw examples of both directly invoking the continuation parameter of OnCompleted and using Task.Run(continuation) in different examples, which should I go for and why?

推荐答案

为什么需要自定义等待器?

Why would you want a custom awaiter?

可以看到编译器对await的解释此处.本质上:

You can see the compiler's interpretation of await here. Essentially:

var temp = e.GetAwaiter();
if (!temp.IsCompleted)
{
  SAVE_STATE()
  temp.OnCompleted(&cont);
  return;

cont:
  RESTORE_STATE()
}
var i = temp.GetResult();

从评论中 OnCompleted 应该将其参数安排为异步操作的延续.

Edit from comments: OnCompleted should schedule its argument as a continuation of the asynchronous operation.

这篇关于虚拟人的自定义等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆