From 49ab47e42adf57e381e826b22c9e8a5f0d0ab7f6 Mon Sep 17 00:00:00 2001 From: kougyoku Date: Tue, 29 Oct 2019 09:39:37 -0700 Subject: [PATCH] Allow only one instance of the app to run. --- QueueSys/Program.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/QueueSys/Program.cs b/QueueSys/Program.cs index c5a963b..1d14ae9 100644 --- a/QueueSys/Program.cs +++ b/QueueSys/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; @@ -14,9 +15,33 @@ namespace QueueSys [STAThread] static void Main() { + if (PriorProcess() != null) + { + + MessageBox.Show("Another instance of the app is already running.","Queue System",MessageBoxButtons.OK, MessageBoxIcon.Warning); + return; + } + Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmEmployeeView()); } + + public static Process PriorProcess() + // Returns a System.Diagnostics.Process pointing to + // a pre-existing process with the same name as the + // current one, if any; or null if the current process + // is unique. + { + Process curr = Process.GetCurrentProcess(); + Process[] procs = Process.GetProcessesByName(curr.ProcessName); + foreach (Process p in procs) + { + if ((p.Id != curr.Id) && + (p.MainModule.FileName == curr.MainModule.FileName)) + return p; + } + return null; + } } }