Can you co_await
in a C++/WinRT TimerElapsedHandler (or any other lambda in C++/WinRT)?
When I try to compile code like this:
auto pointerExitedTimerHandler = winrt::TimerElapsedHandler([](const winrt::ThreadPoolTimer&)
{
co_await 5s;
// Other stuff...
});
I get an error:
error C7588: A definition of a class template std::experimental::task must be provided for the return type of this coroutine to be deduced
How can I write an async handler?
Disclaimer: I work for Microsoft.
You simply need to provide an async return type. For example, -> winrt::fire_and_forget
:
auto pointerExitedTimerHandler = winrt::TimerElapsedHandler([](const winrt::ThreadPoolTimer&) -> winrt::fire_and_forget
{
co_await 5s;
// Other stuff...
});
Fire and forget is a simple WinRT wrapper for async functions that ignores the result—if you need to await the result, you'll need to use a different return type.