Only one instance is allowed to run

This commit is contained in:
2dust 2024-11-03 13:30:32 +08:00
parent 201cfaa922
commit bb661d4f50
3 changed files with 41 additions and 25 deletions

View file

@ -9,8 +9,6 @@ namespace v2rayN.Desktop;
public partial class App : Application
{
//public static EventWaitHandle ProgramStarted;
public override void Initialize()
{
if (!AppHandler.Instance.InitApp())
@ -32,7 +30,7 @@ public partial class App : Application
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
OnStartup(desktop.Args);
AppHandler.Instance.InitComponents();
desktop.Exit += OnExit;
desktop.MainWindow = new MainWindow();
@ -41,22 +39,6 @@ public partial class App : Application
base.OnFrameworkInitializationCompleted();
}
private void OnStartup(string[]? Args)
{
var exePathKey = Utils.GetMd5(Utils.GetExePath());
var rebootas = (Args ?? new string[] { }).Any(t => t == Global.RebootAs);
//ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, exePathKey, out bool bCreatedNew);
//if (!rebootas && !bCreatedNew)
//{
// ProgramStarted.Set();
// Environment.Exit(0);
// return;
//}
AppHandler.Instance.InitComponents();
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject != null)

View file

@ -5,12 +5,44 @@ namespace v2rayN.Desktop;
internal class Program
{
public static EventWaitHandle ProgramStarted;
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
public static void Main(string[] args)
{
OnStartup(args);
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
}
private static void OnStartup(string[]? Args)
{
if (Utils.IsWindows())
{
var exePathKey = Utils.GetMd5(Utils.GetExePath());
var rebootas = (Args ?? new string[] { }).Any(t => t == Global.RebootAs);
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, exePathKey, out bool bCreatedNew);
if (!rebootas && !bCreatedNew)
{
ProgramStarted.Set();
Environment.Exit(0);
return;
}
}
else
{
_ = new Mutex(true, "v2rayN", out var bOnlyOneInstance);
if (!bOnlyOneInstance)
{
Environment.Exit(0);
return;
}
}
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()

View file

@ -29,8 +29,6 @@ namespace v2rayN.Desktop.Views
_config = AppHandler.Instance.Config;
_manager = new WindowNotificationManager(TopLevel.GetTopLevel(this)) { MaxItems = 3, Position = NotificationPosition.BottomRight };
//ThreadPool.RegisterWaitForSingleObject(App.ProgramStarted, OnProgramStarted, null, -1, false);
this.Closing += MainWindow_Closing;
this.KeyDown += MainWindow_KeyDown;
menuSettingsSetUWP.Click += menuSettingsSetUWP_Click;
@ -114,6 +112,8 @@ namespace v2rayN.Desktop.Views
this.Title = $"{Utils.GetVersion()} - {(AppHandler.Instance.IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}";
if (Utils.IsWindows())
{
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
menuGlobalHotkeySetting.IsVisible = false;
}
else
@ -158,7 +158,9 @@ namespace v2rayN.Desktop.Views
private void OnProgramStarted(object state, bool timeout)
{
ShowHideWindow(true);
Dispatcher.UIThread.Post(() =>
ShowHideWindow(true),
DispatcherPriority.Default);
}
private void DelegateSnackMsg(string content)