From 9e4683ef801262f3a96bd70d371f1641b8a3da30 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Mon, 7 Sep 2020 17:18:45 +0200 Subject: [PATCH] Aggiunto Mutex x gestione single startup --- LPA/App.config | 2 ++ LPA/MainForm.cs | 3 ++ LPA/Program.cs | 74 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/LPA/App.config b/LPA/App.config index 8238929..30f431a 100644 --- a/LPA/App.config +++ b/LPA/App.config @@ -17,6 +17,8 @@ + + diff --git a/LPA/MainForm.cs b/LPA/MainForm.cs index 75da25f..0231cf8 100644 --- a/LPA/MainForm.cs +++ b/LPA/MainForm.cs @@ -102,6 +102,8 @@ namespace LPA #region init form + dispose + + /// /// Classe apertura /// @@ -111,6 +113,7 @@ namespace LPA myInit(); checkShowTray(); } + /// /// Medoti init specifici /// diff --git a/LPA/Program.cs b/LPA/Program.cs index 5875188..be6229e 100644 --- a/LPA/Program.cs +++ b/LPA/Program.cs @@ -1,22 +1,72 @@ -using System; +using SteamWare.IO; +using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace LPA { - static class Program - { - /// - /// Punto di ingresso principale dell'applicazione. - /// - [STAThread] - static void Main() + static class Program { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm()); + /// + /// Mutex x evitare multiple instances + /// + static Mutex _m; + static bool IsSingleInstance() + { + try + { + // Try to open existing mutex. + _ = Mutex.OpenExisting("SteamwareLPA"); + } + catch + { + // If exception occurred, there is no such mutex. + Program._m = new Mutex(true, "SteamwareLPA"); + + // Only one instance. + return true; + } + // More than one instance. + return false; + } + + + /// + /// Punto di ingresso principale dell'applicazione. + /// + [STAThread] + static void Main() + { + bool enableStartup = true; + // se abilitato il controllo istanza (solo 1...) + if (memLayer.ML.CRB("singleInstance")) + { + utils.lgInfo("Single instance only: checking!"); + if (!Program.IsSingleInstance()) + { + utils.lgInfo("More than one instance found: exiting"); + enableStartup = false; + } + else + { + // Continue with program. + Console.WriteLine("OK! only One instance running"); + } + } + else + { + utils.lgInfo("Multiple instance allowed: check not requested"); + } + // se abilitato procedo + if (enableStartup) + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new MainForm()); + } + } } - } }