diff --git a/examples/systray-example.rs b/examples/systray-example.rs index 85c1a6e..b976b0a 100644 --- a/examples/systray-example.rs +++ b/examples/systray-example.rs @@ -33,7 +33,7 @@ fn main() -> Result<(), systray::Error> { })?; println!("Waiting on message!"); - app.wait_for_message()?; + app.wait_for_message(None)?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 36c6dbd..bb8f38a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,8 @@ pub mod api; use std::{ collections::HashMap, error, fmt, - sync::mpsc::{channel, Receiver}, + sync::mpsc::{channel, Receiver, RecvTimeoutError}, + time::Duration, }; type BoxedError = Box; @@ -133,12 +134,18 @@ impl Application { self.window.quit() } - pub fn wait_for_message(&mut self) -> Result<(), Error> { + /// + pub fn wait_for_message(&mut self, timeout: Option) -> Result<(), Error> { loop { let msg; - match self.rx.recv() { + match self + .rx + .recv_timeout(timeout.unwrap_or_else(|| Duration::new(u64::MAX, 0))) + { Ok(m) => msg = m, - Err(_) => { + // Yield and wait for the next poll + Err(RecvTimeoutError::Timeout) => break, + Err(RecvTimeoutError::Disconnected) => { self.quit(); break; }