|
@@ -4,6 +4,7 @@ using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Configuration;
|
|
|
using System.Data;
|
|
|
+using System.Diagnostics;
|
|
|
using System.Linq;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
@@ -29,19 +30,24 @@ namespace MeterVision
|
|
|
private static Mutex mutex = new Mutex(true, "{E1805A91-1078-417D-9B65-43FCFCA504CC_ttpp}");
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
|
{
|
|
|
- if (!mutex.WaitOne(TimeSpan.Zero, true))
|
|
|
- {
|
|
|
- //如果另一个实例已经拥有这个互斥锁,那么当前实列将不会继续运行
|
|
|
- MessageBox.Show("程序已经在运行。");
|
|
|
- Current.Shutdown();
|
|
|
- return;
|
|
|
- }
|
|
|
+ //if (!mutex.WaitOne(TimeSpan.Zero, true))
|
|
|
+ //{
|
|
|
+ // //如果另一个实例已经拥有这个互斥锁,那么当前实列将不会继续运行
|
|
|
+ // MessageBox.Show("程序已经在运行。");
|
|
|
+ // Current.Shutdown();
|
|
|
+ // return;
|
|
|
+ //}
|
|
|
|
|
|
base.OnStartup(e);
|
|
|
+
|
|
|
+ int instanceIndex = GetCurrentInstanceIndex();
|
|
|
+ //MessageBox.Show($"当前是第 {instanceIndex + 1} 个实例");
|
|
|
+
|
|
|
this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
|
|
|
|
|
|
|
|
|
MainWindow mainWindow = new MainWindow();
|
|
|
+ mainWindow.InstanceIndex = instanceIndex+1;
|
|
|
this.MainWindow = mainWindow;
|
|
|
|
|
|
LicenseMana.mLicenseModel = new LicenseModel()
|
|
@@ -78,11 +84,11 @@ namespace MeterVision
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
|
{
|
|
|
//释放互斥锁
|
|
|
- if(mutex != null)
|
|
|
- {
|
|
|
- mutex.ReleaseMutex();
|
|
|
- mutex.Close();
|
|
|
- }
|
|
|
+ //if(mutex != null)
|
|
|
+ //{
|
|
|
+ // mutex.ReleaseMutex();
|
|
|
+ // mutex.Close();
|
|
|
+ //}
|
|
|
base.OnExit(e);
|
|
|
}
|
|
|
|
|
@@ -104,7 +110,17 @@ namespace MeterVision
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private int GetCurrentInstanceIndex()
|
|
|
+ {
|
|
|
+ string processName = Process.GetCurrentProcess().ProcessName;
|
|
|
+ var allProcesses = Process.GetProcessesByName(processName)
|
|
|
+ .OrderBy(p => p.StartTime) // 按启动时间排序
|
|
|
+ .ToList();
|
|
|
|
|
|
+ int index = allProcesses.FindIndex(p => p.Id == Process.GetCurrentProcess().Id);
|
|
|
+ return index; // 从0开始,表示第一个、第二个...
|
|
|
+ }
|
|
|
+ ////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
}
|
|
|
}
|