You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.0 KiB
C#

using Lierda.WPFHelper;
using MaterialTraceability.Business;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace MaterialTraceabilityUI
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
System.Threading.Mutex mutex;
LierdaCracker cracker = new LierdaCracker();
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, "ElectronicNeedleTherapySystem", out ret);
mutex = new System.Threading.Mutex(true, System.Diagnostics.Process.GetCurrentProcess().ProcessName, out ret);
if (!ret)
{
MessageBox.Show("应用程序已开启,禁止重复运行");
Environment.Exit(0);
}
}
private void Application_Exit(object sender,ExitEventArgs e)
{
LogHelperBusiness.LogInfo("程序退出");
}
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;
}
/// <summary>
/// Task线程异常捕获
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void TaskScheduler_UnobservedTaskException(object sender,UnobservedTaskExceptionEventArgs e)
{
try
{
var exception = e.Exception as Exception;
if(exception != null)
{
LogHelperBusiness.LogError("Task线程全局异常捕获成功",exception);
}
}catch(Exception ex)
{
LogHelperBusiness.LogError("Task线程全局异常捕获", ex);
}
finally
{
e.SetObserved();
}
}
/// <summary>
/// UI线程异常捕获
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
try
{
LogHelperBusiness.LogError("UI线程全局异常捕获成功", e.Exception);
}
catch (Exception ex)
{
LogHelperBusiness.LogError("UI线程全局异常捕获", ex);
}
finally
{
e.Handled = true;
}
}
/// <summary>
/// 非UI线程异常捕获
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void CurrentDomain_UnhandleException(object sender, UnhandledExceptionEventArgs e)
{
try
{
var exception = e.ExceptionObject as Exception;
if (exception != null)
{
LogHelperBusiness.LogError("非UI线程全局异常捕获成功", exception);
}
}
catch (Exception ex)
{
LogHelperBusiness.LogError("非UI线程全局异常捕获", ex);
}
finally
{
//e.SetObserved();
}
}
}
}