{"record":{"id":"a9275d945a8a2fc3","repo":"babalae/better-genshin-impact","slug":"bettergi","errorCode":null,"errorMessage":"请先启动BetterGI","messagePattern":"请先启动BetterGI","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/GameTask/SystemControl.cs","lineNumber":258,"sourceCode":"        var gameScreenRect = GetGameScreenRect(hWnd);\n        var left = windowRect.Left;\n        var top = windowRect.Top + windowRect.Height - gameScreenRect.Height;\n        var right = left + gameScreenRect.Width;\n        var bottom = top + gameScreenRect.Height;\n        return new RECT(left, top, right, bottom);\n    }\n\n    public static void ActivateWindow(nint hWnd)\n    {\n        User32.ShowWindow(hWnd, ShowWindowCommand.SW_RESTORE);\n        User32.SetForegroundWindow(hWnd);\n    }\n\n    public static void ActivateWindow()\n    {\n        if (!TaskContext.Instance().IsInitialized)\n        {\n            throw new Exception(\"请先启动BetterGI\");\n        }\n\n        ActivateWindow(TaskContext.Instance().GameHandle);\n    }\n    public static void RestartApplication(string[] newArgs)\n    {\n        // 获取当前程序路径\n        string exePath = Process.GetCurrentProcess().MainModule.FileName;\n\n        // 构建参数字符串\n        var restartArgs = new List<string>(newArgs);\n        var instanceType = InstanceBootstrap.Current.Context.InstanceType;\n        if (instanceType == BetterGiInstanceType.ChildSession)\n        {\n            restartArgs.Add(CommandLineOptions.InstanceArgument);\n            restartArgs.Add(\"childSession\");\n        }\n        else if (instanceType == BetterGiInstanceType.WebView)","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/GameTask/SystemControl.cs#L240-L276","documentation":"A generic Exception thrown by the parameterless SystemControl.ActivateWindow() when TaskContext.Instance().IsInitialized is false. It is a hard precondition guard: you cannot bring the game window to the foreground before the application's core game-context has been initialized on the startup page. Unlike NormalEndException, this is a plain System.Exception, so callers must catch broadly.","triggerScenarios":"Calling SystemControl.ActivateWindow() (no argument) before the user has started BetterGI and the capture context on the startup page. Common in features that auto-focus the game window before initialization completes.","commonSituations":"A scheduled task or hotkey handler invoking ActivateWindow during early startup; a feature triggered from a context menu before the screenshotter/capture was started; running a task right after launch.","solutions":["Start BetterGI and initialize the capture context on the startup page before calling this method.","Guard the call site with `if (!TaskContext.Instance().IsInitialized) return;` or show a user-facing prompt.","If you control the flow, await the initialized state before activating.","Catch the exception at the UI boundary and show a ThemedMessageBox telling the user to start BetterGI first."],"exampleFix":"// before\nSystemControl.ActivateWindow();\n// after\nif (!TaskContext.Instance().IsInitialized)\n{\n    await ThemedMessageBox.ShowAsync(\"提示\", \"请先启动BetterGI\");\n    return;\n}\nSystemControl.ActivateWindow();","handlingStrategy":"validation","validationCode":"if (!TaskContext.Instance().IsInitialized)\n{\n    // do not call SystemControl.ActivateWindow()\n    return;\n}","typeGuard":"static bool IsReady => TaskContext.Instance().IsInitialized;","tryCatchPattern":"try { SystemControl.ActivateWindow(); }\ncatch (Exception ex) when (!TaskContext.Instance().IsInitialized)\n{\n    Log.Warning(\"BetterGI 未初始化: {Msg}\", ex.Message);\n}","preventionTips":["Check TaskContext.Instance().IsInitialized before any window-control call.","Start BetterGI on the startup page first.","Disable UI actions that depend on the window until initialized."],"tags":["precondition","task-context","initialization","window-control"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}