Function winmsg_executor::block_on
source · pub fn block_on<F>(future: F) -> Result<F::Output, QuitMessageLoop>Expand description
Runs a future to completion on the calling threads message loop.
This runs the provided future on the current thread, blocking until it is complete. Any tasks spawned which the future spawns internally will be executed no the same thread.
Any spawned tasks will be suspended after block_on returns. Calling
block_on again will resume previously spawned tasks.
§Panics
Panics when the message loops is running already. This happens when
block_on or run is called from async tasks running on this executor.
Examples found in repository?
examples/basic.rs (lines 21-34)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
fn main() {
println!("hello");
block_on(async {
let task = spawn(async {
println!("async hello 1");
poll_n_times(3).await;
println!("async bye 1");
"async 1 result"
});
println!("async hello 2");
poll_n_times(2).await;
println!("async bye 2");
println!("{}", task.await);
})
.unwrap();
println!("bye");
}More examples
examples/threads.rs (lines 22-26)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
fn main() {
thread::spawn(|| {
println!("thread hello");
block_on(async {
println!("thread async hello");
poll_n_times(3).await;
println!("thread async bye");
})
.unwrap();
println!("thread bye");
});
println!("main hello");
block_on(async {
println!("main async hello");
poll_n_times(3).await;
println!("main async bye");
})
.unwrap();
println!("main bye");
}