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.
Aucma.Scada/Aucma.Scada.UI/App.xaml.cs

139 lines
4.0 KiB
C#

using Aucma.Scada.Business;
using HighWayIot.Log4net;
1 year ago
using Lierda.WPFHelper;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace Aucma.Scada.UI
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
private LogHelper logHelper = LogHelper.Instance;
System.Threading.Mutex mutex;
LierdaCracker cracker = new LierdaCracker();
private MainBusiness mainBusiness = MainBusiness.Instance;
1 year ago
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();
}
1 year ago
}
private void Application_Exit(object sender, ExitEventArgs e)
{
mainBusiness.DisConnectPlc();
mainBusiness.ExitHikRobot();
1 year ago
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;
}
/// <summary>
/// Task线程异常捕获
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
/// <summary>
/// UI线程异常捕获
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
/// <summary>
/// 非UI线程异常捕获
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
}
}