c++ - C++/WinRT:我可以等待来自多个协程调用的单个 IAsyncAction 句柄吗?
问题描述
我有 coroutine IAsyncAction Foo
,它将在我的程序开始时调用一次。
我也有协程IAsyncAction Bar
,它会被任意调用多次,并且必须Foo
在某个时候等待。
多个电话可以Bar
等待Foo
这样的单个电话吗?
IAsyncAction m_fooAction = Foo();
(后来,在Bar
……内)
co_await m_fooAction;
我试过这个,但我一直收到错误:(
A delegate was assigned when not allowed
请参阅https://docs.microsoft.com/en-us/uwp/cpp-ref-for-winrt/error-handling/hresult-illegal-delegate-assignment)。
请注意:此开发是针对 Windows 8 桌面应用程序,因此不能使用CoreDispatcher
也不DispatcherQueue
.
解决方案
根据 C++/WinRT 作者的说法,IAsyncAction
类似的接口只能有一个等待者。
作为替代方案,他们建议使用内核句柄。 https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/concurrency-2#awaiting-a-kernel-handle
更新:来自 Raymond Chen (MSFT):
Possible workarounds:
1. Write your own custom object that supports multi-awaiting.
2. Use an existing object that supports multi-awaiting (such as `Concurrency::task`).
3. Use a kernel handle and `resume_on_signal`.
Option 3 is probably simplest.
选项 3 适用于我的情况。
推荐阅读
- php - Laravel API 验证(不同的规则取决于路由)
- sql - from 子句中可以有两个子查询吗?
- c - 奇数加起来不正确
- c++ - 如何为包含另一个类的实例的数组创建一个 const getter?
- c# - 批量更新字典内多条记录的属性(VB.NET / C#)
- java - 在 Eclipse 中找不到资源文件夹,其中 InputStream in = getClass().getResourceAsStream("resources/template.properties");
- python - 尝试从 html 调用视图中的函数时出现 Django“NoReverseMatch”错误
- excel - 我有一个 vba 代码,我想知道运行它的 Excel 文件(工作表)的结构
- javascript - 按下任何按钮都会返回错误:405 Method Not Allowed
- php - Symfony DOMCrawler:如何更改 html?