babalae/better-genshin-impact · error · Exception

请先启动BetterGI

Error message

请先启动BetterGI

What it means

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.

Source

Thrown at BetterGenshinImpact/GameTask/SystemControl.cs:258

        var gameScreenRect = GetGameScreenRect(hWnd);
        var left = windowRect.Left;
        var top = windowRect.Top + windowRect.Height - gameScreenRect.Height;
        var right = left + gameScreenRect.Width;
        var bottom = top + gameScreenRect.Height;
        return new RECT(left, top, right, bottom);
    }

    public static void ActivateWindow(nint hWnd)
    {
        User32.ShowWindow(hWnd, ShowWindowCommand.SW_RESTORE);
        User32.SetForegroundWindow(hWnd);
    }

    public static void ActivateWindow()
    {
        if (!TaskContext.Instance().IsInitialized)
        {
            throw new Exception("请先启动BetterGI");
        }

        ActivateWindow(TaskContext.Instance().GameHandle);
    }
    public static void RestartApplication(string[] newArgs)
    {
        // 获取当前程序路径
        string exePath = Process.GetCurrentProcess().MainModule.FileName;

        // 构建参数字符串
        var restartArgs = new List<string>(newArgs);
        var instanceType = InstanceBootstrap.Current.Context.InstanceType;
        if (instanceType == BetterGiInstanceType.ChildSession)
        {
            restartArgs.Add(CommandLineOptions.InstanceArgument);
            restartArgs.Add("childSession");
        }
        else if (instanceType == BetterGiInstanceType.WebView)

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Start BetterGI and initialize the capture context on the startup page before calling this method.
  2. Guard the call site with `if (!TaskContext.Instance().IsInitialized) return;` or show a user-facing prompt.
  3. If you control the flow, await the initialized state before activating.
  4. Catch the exception at the UI boundary and show a ThemedMessageBox telling the user to start BetterGI first.

Example fix

// before
SystemControl.ActivateWindow();
// after
if (!TaskContext.Instance().IsInitialized)
{
    await ThemedMessageBox.ShowAsync("提示", "请先启动BetterGI");
    return;
}
SystemControl.ActivateWindow();
Defensive patterns

Strategy: validation

Validate before calling

if (!TaskContext.Instance().IsInitialized)
{
    // do not call SystemControl.ActivateWindow()
    return;
}

Type guard

static bool IsReady => TaskContext.Instance().IsInitialized;

Try / catch

try { SystemControl.ActivateWindow(); }
catch (Exception ex) when (!TaskContext.Instance().IsInitialized)
{
    Log.Warning("BetterGI 未初始化: {Msg}", ex.Message);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/a9275d945a8a2fc3. Report an issue: GitHub.