using Aucma.Scada.Business; using HighWayIot.Log4net; using Lierda.WPFHelper; using System; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; namespace Aucma.Scada.UI { /// /// App.xaml 的交互逻辑 /// public partial class App : Application { private LogHelper logHelper = LogHelper.Instance; System.Threading.Mutex mutex; LierdaCracker cracker = new LierdaCracker(); private MainBusiness mainBusiness = MainBusiness.Instance; public App() { this.Startup += new StartupEventHandler(App_StartUp); this.Exit += new ExitEventHandler(Application_Exit); } void App_StartUp(object sender, StartupEventArgs e) { bool ret; mutex = new System.Threading.Mutex(true, System.Diagnostics.Process.GetCurrentProcess().ProcessName, out ret); if (!ret) { MessageBox.Show("应用程序已开启,禁止重复运行"); Environment.Exit(0); } else { mainBusiness.InitPlc(); mainBusiness.InitHikRobot(); } } private void Application_Exit(object sender, ExitEventArgs e) { mainBusiness.DisConnectPlc(); mainBusiness.ExitHikRobot(); logHelper.Info("程序退出"); } protected override void OnStartup(StartupEventArgs e) { cracker.Cracker(100); //设置GC回收间隔 RegisterEvents(); base.OnStartup(e); } private void RegisterEvents() { TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; this.DispatcherUnhandledException += App_DispatcherUnhandledException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandleException; } /// /// Task线程异常捕获 /// /// /// private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) { try { var exception = e.Exception as Exception; if (exception != null) { logHelper.Error("Task线程全局异常捕获成功", exception); } } catch (Exception ex) { logHelper.Error("Task线程全局异常捕获", ex); } finally { e.SetObserved(); } } /// /// UI线程异常捕获 /// /// /// private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { try { logHelper.Error("UI线程全局异常捕获成功", e.Exception); } catch (Exception ex) { logHelper.Error("UI线程全局异常捕获", ex); } finally { e.Handled = true; } } /// /// 非UI线程异常捕获 /// /// /// private void CurrentDomain_UnhandleException(object sender, UnhandledExceptionEventArgs e) { try { var exception = e.ExceptionObject as Exception; if (exception != null) { logHelper.Error("非UI线程全局异常捕获成功", exception); } } catch (Exception ex) { logHelper.Error("非UI线程全局异常捕获", ex); } finally { //e.SetObserved(); } } } }