2021-07-21 11:15:51 +08:00
|
|
|
|
using GeekDesk.Constant;
|
2024-08-07 11:04:48 +08:00
|
|
|
|
using GeekDesk.MyThread;
|
2022-01-09 17:33:49 +08:00
|
|
|
|
using GeekDesk.Util;
|
2024-08-07 11:04:48 +08:00
|
|
|
|
using GeekDesk.ViewModel;
|
|
|
|
|
|
using Microsoft.Win32;
|
2021-07-20 17:30:12 +08:00
|
|
|
|
using System;
|
2024-08-07 11:04:48 +08:00
|
|
|
|
using System.Diagnostics;
|
2021-07-20 17:30:12 +08:00
|
|
|
|
using System.Windows;
|
2022-05-23 17:56:39 +08:00
|
|
|
|
using System.Windows.Input;
|
2024-08-07 11:04:48 +08:00
|
|
|
|
using System.Windows.Interop;
|
|
|
|
|
|
using System.Windows.Media;
|
2022-05-23 17:56:39 +08:00
|
|
|
|
using System.Windows.Threading;
|
2021-04-12 13:46:05 +08:00
|
|
|
|
|
|
|
|
|
|
namespace GeekDesk
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// App.xaml 的交互逻辑
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public partial class App : Application
|
|
|
|
|
|
{
|
2021-07-20 21:12:18 +08:00
|
|
|
|
|
2021-07-20 17:30:12 +08:00
|
|
|
|
System.Threading.Mutex mutex;
|
2021-07-20 21:12:18 +08:00
|
|
|
|
|
|
|
|
|
|
public App()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Startup += new StartupEventHandler(App_Startup);
|
2022-01-09 17:33:49 +08:00
|
|
|
|
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
|
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
2024-08-07 11:04:48 +08:00
|
|
|
|
SystemEvents.PowerModeChanged += OnPowerModeChanged;
|
2021-07-20 21:12:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-07 11:04:48 +08:00
|
|
|
|
|
2021-07-20 17:30:12 +08:00
|
|
|
|
private void App_Startup(object sender, StartupEventArgs e)
|
|
|
|
|
|
{
|
2024-08-07 11:04:48 +08:00
|
|
|
|
//RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly; //禁用硬件加速
|
2021-08-19 13:59:47 +08:00
|
|
|
|
mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out bool ret);
|
2021-07-20 17:30:12 +08:00
|
|
|
|
if (!ret)
|
|
|
|
|
|
{
|
2021-08-19 13:59:47 +08:00
|
|
|
|
System.Threading.Thread.Sleep(2000);
|
|
|
|
|
|
mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out ret);
|
|
|
|
|
|
if (!ret)
|
|
|
|
|
|
{
|
2022-08-30 09:06:27 +08:00
|
|
|
|
MessageUtil.SendMsgByWName(
|
|
|
|
|
|
"GeekDesk_Main_" + Constants.MY_UUID,
|
|
|
|
|
|
"ShowApp"
|
|
|
|
|
|
);
|
2021-08-19 13:59:47 +08:00
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
|
}
|
2021-07-20 17:30:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-07 11:04:48 +08:00
|
|
|
|
|
|
|
|
|
|
//电源监听
|
|
|
|
|
|
private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (e.Mode)
|
|
|
|
|
|
{
|
|
|
|
|
|
case PowerModes.Resume:
|
|
|
|
|
|
// 系统从休眠状态唤醒
|
|
|
|
|
|
LogUtil.WriteLog("System resumed from sleep.");
|
|
|
|
|
|
ProcessUtil.ReStartApp();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case PowerModes.Suspend:
|
|
|
|
|
|
// 系统进入休眠状态
|
|
|
|
|
|
LogUtil.WriteLog("System is going to sleep.");
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 17:33:49 +08:00
|
|
|
|
void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
e.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。
|
2022-05-23 17:56:39 +08:00
|
|
|
|
Mouse.OverrideCursor = null;
|
2022-01-09 17:33:49 +08:00
|
|
|
|
LogUtil.WriteErrorLog(e, "未捕获异常!");
|
|
|
|
|
|
if (Constants.DEV)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show("GeekDesk遇到一个问题, 不用担心, 这不影响其它操作!");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-07-20 17:30:12 +08:00
|
|
|
|
|
2022-01-09 17:33:49 +08:00
|
|
|
|
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogUtil.WriteErrorLog(e, "严重异常!");
|
2023-04-23 21:42:55 +08:00
|
|
|
|
//MessageBox.Show("GeekDesk遇到未知问题崩溃!");
|
2022-01-09 17:33:49 +08:00
|
|
|
|
}
|
2022-05-23 17:56:39 +08:00
|
|
|
|
public static void DoEvents()
|
|
|
|
|
|
{
|
|
|
|
|
|
var frame = new DispatcherFrame();
|
|
|
|
|
|
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
|
|
|
|
|
|
new DispatcherOperationCallback(
|
|
|
|
|
|
delegate (object f)
|
|
|
|
|
|
{
|
|
|
|
|
|
((DispatcherFrame)f).Continue = false;
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}), frame);
|
|
|
|
|
|
Dispatcher.PushFrame(frame);
|
|
|
|
|
|
}
|
2021-07-20 17:30:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 17:33:49 +08:00
|
|
|
|
}
|
2021-07-20 17:30:12 +08:00
|
|
|
|
|
2021-04-12 13:46:05 +08:00
|
|
|
|
}
|