From 8a3fa1426d45ec8baa557f78d3764d3966b0e1fe Mon Sep 17 00:00:00 2001 From: emkael Date: Sat, 31 Aug 2019 17:54:34 +0200 Subject: Ensuring only one app instance is running --- spedytor/Program.cs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/spedytor/Program.cs b/spedytor/Program.cs index aafc441..0cd978c 100644 --- a/spedytor/Program.cs +++ b/spedytor/Program.cs @@ -1,20 +1,34 @@ using System; using System.Collections.Generic; using System.Windows.Forms; +using System.Threading; namespace spedytor { static class Program { + static Mutex mutex = new Mutex(false, "Spedytor - emkael.info"); /// /// The main entry point for the application. /// [STAThread] static void Main() { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new Form1()); + if (!mutex.WaitOne(TimeSpan.FromSeconds(1), false)) + { + MessageBox.Show("Spedytor już działa! Może gdzieś w tle?", "Błąd!", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + finally + { + mutex.ReleaseMutex(); + } } } } -- cgit v1.2.3