c++microsoft-metrowindows-runtimewrl

Can't use create_async() with WRL WinRT component


I want to write a WinRT component with async support using WRL, but I found create_async() is undefined even I included the ppltask.h and using namespace concurrency How to create a background task with WRL pattern?

My function is like:

HRESULT GetStreamAsync([out, retval] IAsyncOperation<IRRandomAccessStream*>** operation);

I also try to use IThreadPoolStatics->RunAsync(), but seems it only can be used in case of IAsyncAction and without return value. Does anyone can help?


Solution

  • concurrency::create_async is only usable in C++/CX (this may not be expressly stated in the documentation, but all of the possible return types are hat types, indicating it is only usable in C++/CX).

    WRL includes the Microsoft::WRL::AsyncBase base class template, which you can derive from to implement asynchronous operations using C++ and WRL.

    I don't know of a canonical implementation that wraps std::future or concurrency::task, but you can find an example of an implementation in the Boost-licensed CxxReflect library (search for async_operation_base and task_based_async_operation).