using MV485.Dlg; using MV485.util; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; namespace MV485 { /// /// App.xaml 的交互逻辑 /// public partial class App : Application { private static Mutex mutex = new Mutex(true, "{E1805A91-1078-417D-9B65-43FCFCA504CC_ddtd}"); protected override void OnStartup(StartupEventArgs e) { if (!mutex.WaitOne(TimeSpan.Zero, true)) { //如果另一个实例已经拥有这个互斥锁,那么当前实列将不会继续运行 MessageBox.Show("程序已经在运行。"); Current.Shutdown(); return; } base.OnStartup(e); this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException); MainWindow mainWindow = new MainWindow(); this.MainWindow = mainWindow; //LicenseMana.mLicenseModel = new LicenseModel() //{ // IsPermanent = true //}; //mainWindow.Show(); // 检查是否已注册 if (LicenseMana.IsLicensed()) { LicenseMana.mIsLicensed = true; // 已注册,显示主界面 mainWindow.Show(); } else { //未注册的话,可以让客户试用 LicenseMana.mIsLicensed = false; DlgTrail dlgTrail = new DlgTrail(); dlgTrail.ShowDialog(); int selectResult = dlgTrail.SelectResult; if(selectResult == 0) { Shutdown(); } else if(selectResult == 2) { mainWindow.Show(); } else if(selectResult == 1) { // 未注册,弹出注册对话框 RegisterDlg registerDlg = new RegisterDlg(); bool? result = registerDlg.ShowDialog(); if (result == true) { // 用户成功注册,显示主界面 LicenseMana.mIsLicensed = true; mainWindow.Show(); } else { Shutdown(); } } }//else } protected override void OnExit(ExitEventArgs e) { //释放互斥锁 if (mutex != null) { mutex.ReleaseMutex(); mutex.Close(); } base.OnExit(e); } private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { // 检查异常类型 if (e.Exception is AccessViolationException) { // 记录或处理访问违规错误 MessageBox.Show("访问无效内存,程序即将停止。", "错误", MessageBoxButton.OK, MessageBoxImage.Error); e.Handled = true; // 防止程序崩溃 } else { // 其他异常处理 MessageBox.Show($"发生错误: {e.Exception.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); e.Handled = true; // 防止程序崩溃 } } //--------------------------------------------- } }