mirror of https://github.com/2dust/v2rayN
已运行检测改用互斥锁 增强可靠性
parent
794b88e5ab
commit
d94fcbd8a3
|
@ -221,6 +221,10 @@ namespace v2rayN
|
||||||
{
|
{
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
|
public static System.Threading.Mutex mutexObj
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Reflection;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using v2rayN.Forms;
|
using v2rayN.Forms;
|
||||||
|
@ -32,9 +31,7 @@ namespace v2rayN
|
||||||
|
|
||||||
//AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
//AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
||||||
|
|
||||||
Thread.Sleep(300); // 尽量避免切换语言后提示“已经运行”
|
if (!IsDuplicateInstance())
|
||||||
Process instance = RunningInstance();
|
|
||||||
if (instance == null)
|
|
||||||
{
|
{
|
||||||
if (!UnzipLibs())
|
if (!UnzipLibs())
|
||||||
{
|
{
|
||||||
|
@ -81,23 +78,17 @@ namespace v2rayN
|
||||||
//}
|
//}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取正在运行的实例,没有运行的实例返回null;
|
/// 检查是否已在运行
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static Process RunningInstance()
|
public static bool IsDuplicateInstance()
|
||||||
{
|
{
|
||||||
Process current = Process.GetCurrentProcess();
|
//string name = "v2rayN";
|
||||||
Process[] processes = Process.GetProcessesByName(current.ProcessName);
|
|
||||||
foreach (Process process in processes)
|
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
|
||||||
if (process.Id != current.Id)
|
|
||||||
{
|
Global.mutexObj = new Mutex(false, name, out bool bCreatedNew);
|
||||||
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == process.MainModule.FileName)
|
return !bCreatedNew;
|
||||||
{
|
|
||||||
return process;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
|
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
|
||||||
|
|
Loading…
Reference in New Issue