【问题标题】:C++/WinRT: Can I await a single IAsyncAction handle from multiple coroutine calls?C++/WinRT:我可以等待来自多个协程调用的单个 IAsyncAction 句柄吗?
【发布时间】:2020-03-20 21:40:09
【问题描述】:

我有协程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 桌面应用程序,因此不能使用 CoreDispatcherDispatcherQueue

【问题讨论】:

    标签: c++ asynchronous c++-winrt


    【解决方案1】:

    根据 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 适用于我的情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-24
      • 2012-08-01
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      相关资源
      最近更新 更多