tokio::signal::windows

Function ctrl_c

Source
pub fn ctrl_c() -> Result<CtrlC>
Available on Windows and crate feature signal only.
Expand description

Creates a new listener which receives “ctrl-c” notifications sent to the process.

§Examples

use tokio::signal::windows::ctrl_c;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // A listener of CTRL-C events.
    let mut signal = ctrl_c()?;

    // Print whenever a CTRL-C event is received.
    for countdown in (0..3).rev() {
        signal.recv().await;
        println!("got CTRL-C. {} more to exit", countdown);
    }

    Ok(())
}