babalae/better-genshin-impact · error · TimeoutException

Raw Input 采集线程初始化超过 {InitializationTimeout.TotalSeconds:0} 秒

Error message

Raw Input 采集线程初始化超过 {InitializationTimeout.TotalSeconds:0} 秒。

What it means

RawInputMonitor spawns a dedicated STA thread that creates a message-only HWND and registers for raw input; StartCore blocks on context.Initialized for InitializationTimeout. If the thread does not signal success within that window, the monitor tears down the context, requests a stop, and throws TimeoutException. The thread may be alive but stuck (e.g. dispatcher not pumping, slow HWND creation) rather than crashed.

Source

Thrown at BetterGenshinImpact/Core/Monitor/RawInputMonitor.cs:64

            _context = context;
        }

        try
        {
            context.Thread.Start();
        }
        catch
        {
            ClearContext(context);
            context.Initialized.Dispose();
            throw;
        }

        if (!context.Initialized.Wait(InitializationTimeout))
        {
            ClearContext(context);
            RequestStop(context);
            throw new TimeoutException(
                $"Raw Input 采集线程初始化超过 {InitializationTimeout.TotalSeconds:0} 秒。");
        }
        context.Initialized.Dispose();

        if (context.InitializationException == null)
        {
            return;
        }

        ClearContext(context);

        throw new InvalidOperationException("Raw Input 初始化失败", context.InitializationException);
    }

    protected override void StopCore()
    {
        RawInputThreadContext? context;
        long stopVersion;

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Retry starting the monitor after a short delay; transient init stalls often clear.
  2. Check for conflicting software (overlays, hooks, screen recorders) and disable them.
  3. Increase InitializationTimeout if the host is consistently slow.
  4. Ensure the process is not starting many monitors concurrently (resource contention).

Example fix

// before
monitor.Start();

// after
try
{
    monitor.Start();
}
catch (TimeoutException)
{
    logger.LogWarning("RawInput 初始化超时,重试一次");
    Thread.Sleep(500);
    monitor.Start();
}
Defensive patterns

Strategy: retry

Try / catch

async Task StartWithRetryAsync(RawInputMonitor m, ILogger log)
{
    for (int attempt = 0; attempt < 2; attempt++)
    {
        try { m.Start(); return; }
        catch (TimeoutException ex)
        {
            log.LogWarning("RawInput 初始化超时 (尝试 {N})", attempt + 1);
            await Task.Delay(500);
        }
    }
    throw;
}

Prevention

When it happens

Trigger: A heavily loaded system where HWND/dispatcher initialization stalls; antivirus hooking thread creation; the STA thread blocked before reaching context.Initialized.Set(); InitializationTimeout set too low.

Common situations: First-run on a slow VM; security software injecting into thread startup; high CPU during game startup delaying the monitor thread; corrupted user32 state.

Understand the failure class

Related errors


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