C++/WinRT:我可以等待来自多个协程调用的单个 IAsyncAction 句柄吗?
C++/WinRT: Can I await a single IAsyncAction handle from multiple coroutine calls?
我有协程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 适合我的情况。
我有协程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 适合我的情况。