Skip to main content

on_main_thread_async

Function on_main_thread_async 

Source
pub async fn on_main_thread_async<R: Send + 'static, F: Future<Output = R> + Send + 'static>(
    debug_label: String,
    future: F,
) -> R
Expand description

Runs the specified future on the main thread and returns its result.

This function can be called from any thread. It submits the given future to the main thread executor and waits for its completion. While the future is executing, the main thread can still process other events, allowing for cooperative multitasking.

§Type Parameters

  • R - The return type of the future, must be Send + 'static
  • F - The future type, must be Send + 'static and return R

§Examples

// Call from any thread to compute on the main thread
let result = app_window::executor::on_main_thread_async("ex".to_owned(),async {
    // This code runs on the main thread
    // Perform computation that needs main thread access
    2 + 2
}).await;

assert_eq!(result, 4);

§Platform Behavior

On all supported platforms, this ensures the future runs on the thread that owns the native event loop, which is required for UI operations.