二次启动拦截,统一了生命进程API
This commit is contained in:
lincube
2026-03-11 09:40:36 +08:00
parent 2781d7e0d9
commit e7a03404ce
21 changed files with 652 additions and 62 deletions

View File

@@ -17,6 +17,15 @@ sealed class Program
AppLogger.Initialize();
RegisterGlobalExceptionLogging();
using var singleInstance = SingleInstanceService.CreateDefault();
if (!singleInstance.IsPrimaryInstance)
{
AppLogger.Warn("Startup", "A secondary launch was blocked because another instance is already running.");
var notified = singleInstance.TryNotifyPrimaryInstance(TimeSpan.FromSeconds(2));
ShowAlreadyRunningNotice(notified);
return;
}
var diagnostics = StartupDiagnosticsService.Run(args);
StartupDiagnosticsService.ShowLegacyExecutableWarningIfNeeded(diagnostics);
@@ -24,6 +33,7 @@ sealed class Program
{
var renderMode = LoadConfiguredRenderMode();
AppLogger.Info("Startup", $"Resolved render mode '{renderMode}'.");
App.CurrentSingleInstanceService = singleInstance;
BuildAvaloniaApp(renderMode).StartWithClassicDesktopLifetime(args);
AppLogger.Info("Startup", "Application exited normally.");
}
@@ -32,6 +42,10 @@ sealed class Program
AppLogger.Critical("Startup", "Application terminated during startup.", ex);
throw;
}
finally
{
App.CurrentSingleInstanceService = null;
}
}
// Avalonia configuration, don't remove; also used by visual designer.
@@ -71,6 +85,16 @@ sealed class Program
}
}
private static void ShowAlreadyRunningNotice(bool notifiedPrimaryInstance)
{
const string caption = "LanMountainDesktop";
var message = notifiedPrimaryInstance
? "应用已打开,不需要多开了。\r\n\r\n已为你切换到正在运行的阑山桌面。"
: "应用已打开,不需要多开了。\r\n\r\n请切换到正在运行的阑山桌面。";
WindowsNativeDialogService.ShowInformation(caption, message);
}
private static void RegisterGlobalExceptionLogging()
{
AppDomain.CurrentDomain.UnhandledException += (_, eventArgs) =>