97 lines
2.9 KiB
C#
97 lines
2.9 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
|
|
namespace AutoUpdateEx
|
|
{
|
|
/// <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()
|
|
{
|
|
DispatcherUnhandledException += App_DispatcherUnhandledException;
|
|
Startup += new StartupEventHandler(App_Startup);
|
|
}
|
|
|
|
/// <summary>
|
|
/// UI线程异常处理事件
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
Utils.LoggerHelper.WriteLogger(e.Exception.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 非UI线程异常处理事件
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
{
|
|
Utils.LoggerHelper.WriteLogger((e.ExceptionObject as Exception).ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 程序启动初始化
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
void App_Startup(object sender, StartupEventArgs e)
|
|
{
|
|
mutex = new System.Threading.Mutex(true, "AutoUpdateEx", out bool ret);
|
|
if (!ret)
|
|
{
|
|
ActivateOtherWindow();
|
|
Environment.Exit(0);
|
|
}
|
|
else
|
|
{
|
|
MainWindow _MainWindow = new MainWindow();
|
|
_MainWindow.Show();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换窗口
|
|
/// </summary>
|
|
private static void ActivateOtherWindow()
|
|
{
|
|
var other = FindWindow(null, "系统更新");
|
|
if (other != IntPtr.Zero)
|
|
{
|
|
SetForegroundWindow(other);
|
|
if (IsIconic(other))
|
|
OpenIcon(other);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动事件
|
|
/// </summary>
|
|
/// <param name="e"></param>
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
System.Windows.Forms.Application.EnableVisualStyles();
|
|
}
|
|
}
|
|
}
|