ZeroLauncher/App.xaml.cs

99 lines
3.0 KiB
C#
Raw Normal View History

2024-03-18 22:59:51 +08:00
using System.Diagnostics;
using System.IO;
2024-03-22 17:23:15 +08:00
using System.Runtime.InteropServices;
2024-03-18 22:59:51 +08:00
using System.Windows;
2024-03-07 21:04:59 +08:00
using Zerolauncher.Manager;
namespace Zerolauncher
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
2024-03-18 22:59:51 +08:00
public static bool needUpdate = false;
2024-03-22 17:23:15 +08:00
static Mutex? mutex;
2024-03-18 22:59:51 +08:00
static bool TryDeleteDirectory(string path, bool recursive)
{
try
{
Directory.Delete(path, recursive);
return true;
}
2024-03-19 12:09:31 +08:00
catch (UnauthorizedAccessException)
2024-03-18 22:59:51 +08:00
{
return false;
}
}
2024-03-22 17:23:15 +08:00
static bool CheckProcess()
{
Process[] processes = Process.GetProcesses();
Process currentProcess = Process.GetCurrentProcess();
bool processExist = false;
foreach (Process p in processes)
{
if (p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id)
{
processExist = true;
}
}
return processExist;
}
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
2024-03-07 21:04:59 +08:00
protected override void OnStartup(StartupEventArgs e)
{
2024-03-22 17:23:15 +08:00
if (CheckProcess())
{
Trace.WriteLine("应用程序已经在运行,将其置于前台并退出");
// 应用程序已经在运行,将其置于前台并退出
var currentProcess = Process.GetCurrentProcess();
foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if (process.Id != currentProcess.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
// 关闭当前的应用程序实例
Shutdown();
return;
}
2024-03-07 21:04:59 +08:00
base.OnStartup(e);
2024-03-18 22:59:51 +08:00
2024-03-11 10:57:12 +08:00
Task.Run(async () =>
2024-03-09 18:29:16 +08:00
{
2024-03-11 10:57:12 +08:00
await CloundMananger.TakeQMessage();
UpDateManager.DoCheckUpdate();
2024-03-18 22:59:51 +08:00
if (UpDateManager.state)
2024-03-11 10:57:12 +08:00
{
2024-03-18 22:59:51 +08:00
UpDateManager.DoUpdate();
2024-03-11 10:57:12 +08:00
}
});
2024-03-19 12:09:31 +08:00
Task.Run(async () =>
{
if (Directory.Exists("./cache/"))
{
for (int i = 0; i < 3 && !TryDeleteDirectory(@"./cache", true); i++) await Task.Delay(3000);
}
});
2024-03-11 10:57:12 +08:00
DataStream.Load();
_ = WebApiManager.StartListener();
2024-03-19 12:09:31 +08:00
AccountManager.initLoadData();
}
2024-03-09 18:29:16 +08:00
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
WebApiManager.StopListener();
2024-03-18 22:59:51 +08:00
if (needUpdate) Process.Start("./cache/upacket.bin");
2024-03-07 21:04:59 +08:00
}
}
}