example2/
example2.rs

1use cancellable_timer::*;
2use std::io;
3use std::time::Duration;
4
5fn main() {
6    let callback = |status: io::Result<()>| match status {
7        Ok(_) => println!("Called"),
8        Err(ref e) if e.kind() == io::ErrorKind::Interrupted => println!("Cancelled"),
9        Err(e) => eprintln!("Error: {:?}", e),
10    };
11
12    println!("Run callback after 2s");
13    Timer::after(Duration::from_secs(2), callback).unwrap();
14
15    println!("Wait 4s");
16    std::thread::sleep(Duration::from_secs(4));
17
18    println!("Run callback after 4s");
19    let canceller = Timer::after(Duration::from_secs(4), callback).unwrap();
20
21    std::thread::sleep(Duration::from_secs(2));
22    println!("Cancel timer.");
23    canceller.cancel().unwrap();
24
25    std::thread::sleep(Duration::from_secs(3));
26}