Search code examples
c++multithreadingconcurrencyasync-awaitwinrt-async

winrt/c++: await result from dispatched task


I want to dispatch a job to the UI thread, then wait for the result and use it from another thread. Like this, but co_await does not work inside the lambda:

dispatcher.RunAsync(CoreDispatcherPriority::Normal, [&]() {
    auto res = co_await mStoreContext.RequestPurchaseAsync(L"");
});

Or even waiting for the whole RunAsync operation, if I could get my result out from it


Solution

  • That's because void can not be used as a return value from a coroutine (I can be if you use my my library).

    try returning a std::future<void> instead:

    dispatcher.RunAsync(CoreDispatcherPriority::Normal, [&]() -> std::future<void> {
        auto res = co_await mStoreContext.RequestPurchaseAsync(L"");
    });