App.xaml.cs 3.7 KB

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