using IvfTl.Control.Services;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace TLTest
{
///
/// Interaction logic for App.xaml
///
public partial class App : Application
{
private static Mutex instance;
///
/// 分支说明:15分钟定时换气,下位机不能接管换气
///
public App()
{
Log4netHelper.WriteLog("程序启动");
//首先注册开始和退出事件
this.Startup += new StartupEventHandler(App_Startup);
//this.Exit += new ExitEventHandler(App_Exit);
}
protected override void OnStartup(StartupEventArgs e)
{
bool isNotRunning; //互斥体判断
instance = new Mutex(true, "ivf_tl_ControlMain", out isNotRunning); //同步基元变量
if (!isNotRunning) // 如果不是未运行状态
{
MessageBox.Show("程序已启动 ");
App.Current.Shutdown();
return;
}
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
Log4netHelper.WriteLog("程序退出");
}
private void App_Exit(object sender, ExitEventArgs e)
{
Log4netHelper.WriteLog("App_Exit");
}
private void App_Startup(object sender, StartupEventArgs e)
{
//UI线程未捕获异常处理事件
this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
//Task线程内未捕获异常处理事件
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
//非UI线程未捕获异常处理事件
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
ChangeLanguage(ConfigurationManager.AppSettings["Language"].ToString());
}
///
/// 非UI线程未捕获异常处理事件
///
///
///
///
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
if (e.IsTerminating)
{
Log4netHelper.WriteLog($"非UI线程发生致命错误,程序即将终止");
}
if (e.ExceptionObject is Exception ex)
{
if (ex.InnerException != null)
{
Log4netHelper.WriteLog($"非UI线程异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
}
Log4netHelper.WriteLog($"非UI线程异常:{ex.Message}{ex.StackTrace}");
}
else
{
Log4netHelper.WriteLog($"非UI线程异常:异常对象类型不是Exception");
}
}
catch (Exception exx)
{
if (exx.InnerException != null)
{
Log4netHelper.WriteLog($"捕获非UI线程异常时发生异常详细:{exx.InnerException.Message}{exx.InnerException.StackTrace}");
}
Log4netHelper.WriteLog($"捕获非UI线程异常时发生异常:{exx.Message}{exx.StackTrace}");
}
}
///
/// Task线程内未捕获异常处理事件
///
///
///
///
private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
try
{
if (e.Exception.InnerException != null)
{
Log4netHelper.WriteLog($"Task线程异常详细:{e.Exception.InnerException.Message}{e.Exception.InnerException.StackTrace}");
}
Log4netHelper.WriteLog($"Task线程异常:{e.Exception.Message}{e.Exception.StackTrace}");
e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
}
catch (Exception ex)
{
if (ex.InnerException != null)
{
Log4netHelper.WriteLog($"捕获Task线程异常时发生异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
}
Log4netHelper.WriteLog($"捕获Task线程异常时发生异常:{ex.Message}{ex.StackTrace}");
}
finally
{
e.SetObserved();//设置该异常已察觉(这样处理后就不会引起程序崩溃)
}
}
///
/// UI线程未捕获异常处理事件
///
///
///
///
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
try
{
if (e.Exception.InnerException != null)
{
Log4netHelper.WriteLog($"UI线程异常详细:{e.Exception.InnerException.Message}{e.Exception.InnerException.StackTrace}");
}
Log4netHelper.WriteLog($"UI线程异常:{e.Exception.Message}{e.Exception.StackTrace}");
//把 Handled 属性设为true,表示此异常已处理,程序可以继续运行,不会强制退出
}
catch (Exception ex)
{
//此时程序出现严重异常,将强制结束退出
if (ex.InnerException != null)
{
Log4netHelper.WriteLog($"捕获UI线程异常时发生异常详细:{ex.InnerException.Message}{ex.InnerException.StackTrace}");
}
Log4netHelper.WriteLog($"捕获UI线程异常时发生异常:{ex.Message}{ex.StackTrace}");
}
finally
{
e.Handled = true;
}
}
public void ChangeLanguage(string languageName)
{
try
{
ResourceDictionary langRd = null;
#if DEBUG
string xamlFilePath = @"C:\PersonalSpace\work\1 VisualWorkSpace\ControlLan\" + languageName;
#else
string xamlFilePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}Language\\{languageName}";
#endif
if (!File.Exists(xamlFilePath))
{
Log4netHelper.WriteLog($"切换语言失败,配置文件不存在:{xamlFilePath}");
return;
}
using (var stream = new FileStream(xamlFilePath, FileMode.Open))
{
langRd = System.Windows.Markup.XamlReader.Load(stream) as ResourceDictionary;
}
if (langRd != null)
{
int count = Application.Current.Resources.MergedDictionaries.Count;
if (count >= 3)
{
Application.Current.Resources.MergedDictionaries.RemoveAt(count - 1);
}
Application.Current.Resources.MergedDictionaries.Add(langRd);
}
else
{
Log4netHelper.WriteLog($"切换语言失败,文件转ResourceDictionary失败;{xamlFilePath}");
}
}
catch (Exception ex)
{
Log4netHelper.WriteLog($"切换语言异常,{JsonConvert.SerializeObject(ex)}");
return;
}
}
}
}