pub fn single_instance(app_id: &str) -> Result<Option<InstanceGuard>>Expand description
Attempts to acquire a named process-wide single-instance guard.
Returns Ok(Some(InstanceGuard)) for the first instance and Ok(None) if another
instance with the same app_id is already running.
The mutex name is derived from app_id using a Local\... namespace, so the
single-instance behavior is scoped to the current Windows session.
ยงErrors
Returns Error::InvalidInput if app_id is empty or whitespace only.
Returns Error::WindowsApi if CreateMutexW fails.
Examples found in repository?
examples/single_instance.rs (line 2)
1fn main() {
2 match win_desktop_utils::single_instance("demo-app").unwrap() {
3 Some(_guard) => {
4 println!("first instance");
5 println!("press Enter to exit");
6 let mut s = String::new();
7 std::io::stdin().read_line(&mut s).unwrap();
8 }
9 None => {
10 println!("already running");
11 }
12 }
13}