Skip to content

Commit

Permalink
Nicer handling of multi-instance
Browse files Browse the repository at this point in the history
  • Loading branch information
BobVul committed Nov 14, 2014
1 parent c11db97 commit c4d8fe6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions IdleShutdown/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ static void Main(string[] args)
hasMutex = true;
}

if (persistent && !hasMutex)
{
MessageBox.Show("A persistent instance can only be started when no other instances are running.");
return;
}

if (!persistent)
{
if (!hasMutex)
Expand All @@ -72,6 +78,10 @@ static void Main(string[] args)
}
else
{
// we only use the mutex for checking if persistent exists
// otherwise, allow multiple non-persistent instances by releasing the mutex *before* timer starts
instanceMutex.ReleaseMutex();

MainForm form = new MainForm();
form.InitialDelay = TimeSpan.FromMinutes(initialDelay);
form.ExitOnAbort = true;
Expand All @@ -88,10 +98,10 @@ static void Main(string[] args)
// force the creation of a handle
IntPtr temp = form.Handle;
Application.Run();
}

if (hasMutex)
// for persistent instances release the mutex *after* it's finished/closed
instanceMutex.ReleaseMutex();
}
}
}
}

0 comments on commit c4d8fe6

Please sign in to comment.