123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- 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
- {
- /// <summary>
- /// App.xaml 的交互逻辑
- /// </summary>
- 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; // 防止程序崩溃
- }
- }
- //---------------------------------------------
- }
- }
|