0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I can't mark it as accepted for a few hours, since it's my own answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.