2025-03-28 09:49:56 +08:00

96 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
namespace ConnectPoint
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
[DllImport("user32", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindow(string cls, string win);
[DllImport("user32")]
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32")]
static extern bool OpenIcon(IntPtr hWnd);
System.Threading.Mutex mutex;
/// <summary>
/// 程序入口
/// </summary>
public App()
{
//UI线程异常处理事件
DispatcherUnhandledException += App_DispatcherUnhandledException;
//非UI线程未捕获异常处理事件
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Startup += new StartupEventHandler(App_Startup);
}
/// <summary>
/// UI线程异常处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//MessageBox.Show(e.Exception.ToString());
//Shutdown(1);
try
{
ESSupport.Lib.LogHelper.WriteServiceLog($"状态反馈程序主线程异常。原因:[{e.Exception.Source}]{e.Exception.Message}");
}
catch { }
e.Handled = true;
Environment.Exit(0);
}
/// <summary>
/// 非UI线程异常处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
//ESSupport.Lib.LogHelper.WriteServiceLog((e.ExceptionObject as Exception).ToString());
ESSupport.Lib.LogHelper.WriteServiceLog($"状态反馈程序子线程异常。原因:[{(e.ExceptionObject as Exception).Source}]{(e.ExceptionObject as Exception).Message}");
}
catch { }
}
/// <summary>
/// 程序启动初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void App_Startup(object sender, StartupEventArgs e)
{
mutex = new System.Threading.Mutex(true, "ConnectPoint", out bool ret);
if (!ret)
{
//ActivateOtherWindow();
Environment.Exit(0);
}
else
{
MainWindow _MainWindow = new MainWindow
{
WindowStyle = WindowStyle.None,
WindowState = WindowState.Minimized,
ShowInTaskbar = false,
};
_MainWindow.Show();
}
}
}
}