Skip to content

ui::FireAndForget

Header: winui/ui/Coroutine.hpp.

The return type of a coroutine that is started and not waited on, such as a click handler that loads something on a worker and shows it when it is done. Write the work top to bottom and hop between threads with co_await ui::on_background() and co_await ui::on_ui_thread(home).

The coroutine runs on the calling thread up to its first co_await, then wherever the awaited hop resumes it. Its frame frees itself when it finishes, or when a hop drops it (see on_ui_thread()). An exception that escapes it calls std::terminate(), since nothing is left to catch it, so catch exceptions inside the coroutine. The handle it returns holds nothing.

Example:

// `status` is app state that outlives the window, shown by a `ui::bind(status)`.
ui::FireAndForget load(ui::Window& window, ui::Property<std::string>& status, std::string path)
{
    status = "Loading...";
    const ui::WindowRef home = window.ref();
    co_await ui::on_background(); // from here on a worker thread: no tree or property access
    std::string text = read_file(path);
    co_await ui::on_ui_thread(home); // back on the window's thread, inside tick()
    status = std::move(text);
}

struct promise_type

What the compiler needs to make a coroutine of this type.

get_return_object

FireAndForget get_return_object() const noexcept

The (empty) handle the call returns.

initial_suspend

std::suspend_never initial_suspend() const noexcept

Runs at once, on the calling thread.

final_suspend

std::suspend_never final_suspend() const noexcept

The frame frees itself at the end.

return_void

void return_void() const noexcept

Nothing to hand back.

unhandled_exception

void unhandled_exception() const noexcept

An exception escaping the coroutine ends the program; see the class comment.