babalae/better-genshin-impact · error · Exception

请先在启动页启动BetterGI,如果已经启动请重启

Error message

请先在启动页启动BetterGI,如果已经启动请重启

What it means

A generic Exception thrown by the TaskTriggerDispatcher.Instance() singleton accessor when the static _instance field is null. It indicates the dispatcher was never constructed (the startup page did not run) or was torn down. This is the canonical 'BetterGI not started' guard used throughout the realtime-trigger pipeline.

Source

Thrown at BetterGenshinImpact/GameTask/TaskTriggerDispatcher.cs:77

        public event EventHandler? UiTaskStartTickEvent;

        private GameUiCategory PrevGameUiCategory = GameUiCategory.Unknown; // 上一个UI类别
        private DateTime PrevGameUiChangeTime = DateTime.Now; // 上一次UI变化时间
        

        public TaskTriggerDispatcher()
        {
            _instance = this;
            _timer.Elapsed += Tick;
            //_timer.Tick += Tick;
        }

        public static TaskTriggerDispatcher Instance()
        {
            if (_instance == null)
            {
                throw new Exception("请先在启动页启动BetterGI,如果已经启动请重启");
            }

            return _instance;
        }

        public static IGameCapture GlobalGameCapture
        {
            get
            {
                _instance = Instance();

                if (_instance.GameCapture == null)
                {
                    throw new Exception("截图器未初始化!");
                }

                return _instance.GameCapture;
            }

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Start BetterGI on the startup page so the dispatcher is constructed; if it was already started, restart it.
  2. Guard callers with a TaskContext.Instance().IsInitialized check before touching the dispatcher.
  3. Avoid calling Instance() from background/shutdown paths.
  4. Catch the exception at the UI boundary and prompt the user to start BetterGI.

Example fix

// before
var cap = TaskTriggerDispatcher.GlobalGameCapture;
// after
if (!TaskContext.Instance().IsInitialized)
{
    throw new NormalEndException("请先在启动页启动BetterGI");
}
var cap = TaskTriggerDispatcher.GlobalGameCapture;
Defensive patterns

Strategy: validation

Validate before calling

if (!TaskContext.Instance().IsInitialized)
{
    // avoid TaskTriggerDispatcher.Instance()
    return;
}

Type guard

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

Try / catch

try { var d = TaskTriggerDispatcher.Instance(); }
catch (Exception ex) { Log.Warning("Dispatcher 未就绪: {Msg}", ex.Message); }

Prevention

When it happens

Trigger: Calling TaskTriggerDispatcher.Instance() (directly or via GlobalGameCapture) before the dispatcher singleton has been created during startup, or after it has been set to null on shutdown.

Common situations: A feature or ViewModel querying the dispatcher during early startup or after the app is closing; multi-instance scenarios where the wrong instance is queried; a regression that cleared _instance prematurely.

Related errors


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