App.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using MeterVision.Dlg;
  2. using MeterVision.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Threading;
  13. namespace MeterVision
  14. {
  15. /// <summary>
  16. /// App.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class App : Application
  19. {
  20. // protected override void OnStartup(StartupEventArgs e)
  21. //{
  22. // base.OnStartup(e);
  23. // this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
  24. // // 设置许可证上下文为非商业用途
  25. // //ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  26. //}
  27. private static Mutex mutex = new Mutex(true, "{E1805A91-1078-417D-9B65-43FCFCA504CC_ttpp}");
  28. protected override void OnStartup(StartupEventArgs e)
  29. {
  30. //if (!mutex.WaitOne(TimeSpan.Zero, true))
  31. //{
  32. // //如果另一个实例已经拥有这个互斥锁,那么当前实列将不会继续运行
  33. // MessageBox.Show("程序已经在运行。");
  34. // Current.Shutdown();
  35. // return;
  36. //}
  37. base.OnStartup(e);
  38. int instanceIndex = GetCurrentInstanceIndex();
  39. //MessageBox.Show($"当前是第 {instanceIndex + 1} 个实例");
  40. this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
  41. MainWindow mainWindow = new MainWindow();
  42. mainWindow.InstanceIndex = instanceIndex+1;
  43. this.MainWindow = mainWindow;
  44. LicenseMana.mLicenseModel = new LicenseModel()
  45. {
  46. IsPermanent = true
  47. };
  48. mainWindow.Show();
  49. // 检查是否已注册
  50. //if (LicenseMana.IsLicensed())
  51. //{
  52. // // 已注册,显示主界面
  53. // mainWindow.Show();
  54. //}
  55. //else
  56. //{
  57. // // 未注册,弹出注册对话框
  58. // RegisterDlg registerDlg = new RegisterDlg();
  59. // bool? result = registerDlg.ShowDialog();
  60. // if (result == true)
  61. // {
  62. // // 用户成功注册,显示主界面
  63. // mainWindow.Show();
  64. // }
  65. // else
  66. // {
  67. // Shutdown();
  68. // }
  69. //}
  70. //---------------------------------------------------------------------------------
  71. }
  72. protected override void OnExit(ExitEventArgs e)
  73. {
  74. //释放互斥锁
  75. //if(mutex != null)
  76. //{
  77. // mutex.ReleaseMutex();
  78. // mutex.Close();
  79. //}
  80. base.OnExit(e);
  81. }
  82. private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  83. {
  84. // 检查异常类型
  85. if (e.Exception is AccessViolationException)
  86. {
  87. // 记录或处理访问违规错误
  88. MessageBox.Show("访问无效内存,程序即将停止。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  89. e.Handled = true; // 防止程序崩溃
  90. }
  91. else
  92. {
  93. // 其他异常处理
  94. MessageBox.Show($"发生错误: {e.Exception.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  95. e.Handled = true; // 防止程序崩溃
  96. }
  97. }
  98. private int GetCurrentInstanceIndex()
  99. {
  100. string processName = Process.GetCurrentProcess().ProcessName;
  101. var allProcesses = Process.GetProcessesByName(processName)
  102. .OrderBy(p => p.StartTime) // 按启动时间排序
  103. .ToList();
  104. int index = allProcesses.FindIndex(p => p.Id == Process.GetCurrentProcess().Id);
  105. return index; // 从0开始,表示第一个、第二个...
  106. }
  107. ////////////////////////////////////////////////////////////////////////////////
  108. }
  109. }