improve:卡死检测

This commit is contained in:
2026-01-19 18:16:03 +08:00
parent 3f17cc705b
commit 02fe33da5a
+10 -8
View File
@@ -642,8 +642,8 @@ namespace Ink_Canvas
async void App_Startup(object sender, StartupEventArgs e) async void App_Startup(object sender, StartupEventArgs e)
{ {
// 初始化应用启动时间
appStartTime = DateTime.Now; appStartTime = DateTime.Now;
appStartupStartTime = DateTime.Now;
// 根据设置决定是否显示启动画面 // 根据设置决定是否显示启动画面
if (ShouldShowSplashScreen()) if (ShouldShowSplashScreen())
@@ -1078,20 +1078,23 @@ namespace Ink_Canvas
private static bool isStartupComplete = false; private static bool isStartupComplete = false;
private static DateTime startupCompleteHeartbeat = DateTime.MinValue; private static DateTime startupCompleteHeartbeat = DateTime.MinValue;
private static DateTime splashScreenStartTime = DateTime.MinValue; private static DateTime splashScreenStartTime = DateTime.MinValue;
private static DateTime appStartupStartTime = DateTime.MinValue;
private void StartHeartbeatMonitor() private void StartHeartbeatMonitor()
{ {
heartbeatTimer = new Timer(_ => lastHeartbeat = DateTime.Now, null, 0, 1000); heartbeatTimer = new Timer(_ => lastHeartbeat = DateTime.Now, null, 0, 1000);
watchdogTimer = new Timer(_ => watchdogTimer = new Timer(_ =>
{ {
if (_isSplashScreenShown && splashScreenStartTime != DateTime.MinValue) if (!isStartupComplete && appStartupStartTime != DateTime.MinValue)
{ {
if (!isStartupComplete) DateTime startTime = _isSplashScreenShown && splashScreenStartTime != DateTime.MinValue
? splashScreenStartTime
: appStartupStartTime;
TimeSpan elapsedSinceStart = DateTime.Now - startTime;
if (elapsedSinceStart.TotalMinutes >= 2)
{ {
TimeSpan elapsedSinceSplashStart = DateTime.Now - splashScreenStartTime; string timeType = _isSplashScreenShown ? "启动画面已显示" : "应用启动开始";
if (elapsedSinceSplashStart.TotalMinutes >= 2) LogHelper.WriteLogToFile($"检测到启动假死:{timeType}{elapsedSinceStart.TotalMinutes:F2}分钟,但未收到启动完成心跳,自动重启。", LogHelper.LogType.Error);
{
LogHelper.WriteLogToFile($"检测到启动假死:启动画面已显示{elapsedSinceSplashStart.TotalMinutes:F2}分钟,但未收到启动完成心跳,自动重启。", LogHelper.LogType.Error);
SyncCrashActionFromSettings(); SyncCrashActionFromSettings();
if (CrashAction == CrashActionType.SilentRestart) if (CrashAction == CrashActionType.SilentRestart)
{ {
@@ -1113,7 +1116,6 @@ namespace Ink_Canvas
return; return;
} }
} }
}
if (isStartupComplete && (DateTime.Now - lastHeartbeat).TotalSeconds > 10) if (isStartupComplete && (DateTime.Now - lastHeartbeat).TotalSeconds > 10)
{ {