using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Threading;
namespace AutoUpdateEx
{
///
/// App.xaml 的交互逻辑
///
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;
///
/// 程序入口
///
public App()
{
DispatcherUnhandledException += App_DispatcherUnhandledException;
Startup += new StartupEventHandler(App_Startup);
}
///
/// UI线程异常处理事件
///
///
///
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Utils.LoggerHelper.WriteLogger(e.Exception.ToString());
}
///
/// 非UI线程异常处理事件
///
///
///
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Utils.LoggerHelper.WriteLogger((e.ExceptionObject as Exception).ToString());
}
///
/// 程序启动初始化
///
///
///
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();
}
}
///
/// 切换窗口
///
private static void ActivateOtherWindow()
{
var other = FindWindow(null, "系统更新");
if (other != IntPtr.Zero)
{
SetForegroundWindow(other);
if (IsIconic(other))
OpenIcon(other);
}
}
///
/// 启动事件
///
///
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
System.Windows.Forms.Application.EnableVisualStyles();
}
}
}