From d94fcbd8a35d42065fcf54f128c17bd972e794ae Mon Sep 17 00:00:00 2001 From: YFdyh000 Date: Mon, 16 Mar 2020 03:40:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=B2=E8=BF=90=E8=A1=8C=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E6=94=B9=E7=94=A8=E4=BA=92=E6=96=A5=E9=94=81=20=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E5=8F=AF=E9=9D=A0=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/Global.cs | 4 ++++ v2rayN/v2rayN/Program.cs | 29 ++++++++++------------------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 68bef5ae..3f1cd7a7 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -221,6 +221,10 @@ namespace v2rayN { get; set; } + public static System.Threading.Mutex mutexObj + { + get; set; + } #endregion diff --git a/v2rayN/v2rayN/Program.cs b/v2rayN/v2rayN/Program.cs index 13b666db..133e2108 100644 --- a/v2rayN/v2rayN/Program.cs +++ b/v2rayN/v2rayN/Program.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics; -using System.Reflection; using System.Threading; using System.Windows.Forms; using v2rayN.Forms; @@ -32,9 +31,7 @@ namespace v2rayN //AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; - Thread.Sleep(300); // 尽量避免切换语言后提示“已经运行” - Process instance = RunningInstance(); - if (instance == null) + if (!IsDuplicateInstance()) { if (!UnzipLibs()) { @@ -81,23 +78,17 @@ namespace v2rayN //} /// - /// 获取正在运行的实例,没有运行的实例返回null; + /// 检查是否已在运行 /// - public static Process RunningInstance() + public static bool IsDuplicateInstance() { - Process current = Process.GetCurrentProcess(); - Process[] processes = Process.GetProcessesByName(current.ProcessName); - foreach (Process process in processes) - { - if (process.Id != current.Id) - { - if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == process.MainModule.FileName) - { - return process; - } - } - } - return null; + //string name = "v2rayN"; + + string name = Utils.GetExePath(); // Allow different locations to run + name = name.Replace("\\", "/"); // https://stackoverflow.com/questions/20714120/could-not-find-a-part-of-the-path-error-while-creating-mutex + + Global.mutexObj = new Mutex(false, name, out bool bCreatedNew); + return !bCreatedNew; } static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)