{"record":{"id":"c54b59172566e2c8","repo":"babalae/better-genshin-impact","slug":"raw-input-initializationtimeout-totalse","errorCode":null,"errorMessage":"Raw Input 采集线程初始化超过 {InitializationTimeout.TotalSeconds:0} 秒。","messagePattern":"Raw Input 采集线程初始化超过 (.+?) 秒。","errorType":"exception","errorClass":"TimeoutException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Monitor/RawInputMonitor.cs","lineNumber":64,"sourceCode":"            _context = context;\n        }\n\n        try\n        {\n            context.Thread.Start();\n        }\n        catch\n        {\n            ClearContext(context);\n            context.Initialized.Dispose();\n            throw;\n        }\n\n        if (!context.Initialized.Wait(InitializationTimeout))\n        {\n            ClearContext(context);\n            RequestStop(context);\n            throw new TimeoutException(\n                $\"Raw Input 采集线程初始化超过 {InitializationTimeout.TotalSeconds:0} 秒。\");\n        }\n        context.Initialized.Dispose();\n\n        if (context.InitializationException == null)\n        {\n            return;\n        }\n\n        ClearContext(context);\n\n        throw new InvalidOperationException(\"Raw Input 初始化失败\", context.InitializationException);\n    }\n\n    protected override void StopCore()\n    {\n        RawInputThreadContext? context;\n        long stopVersion;","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Monitor/RawInputMonitor.cs#L46-L82","documentation":"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.","triggerScenarios":"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.","commonSituations":"First-run on a slow VM; security software injecting into thread startup; high CPU during game startup delaying the monitor thread; corrupted user32 state.","solutions":["Retry starting the monitor after a short delay; transient init stalls often clear.","Check for conflicting software (overlays, hooks, screen recorders) and disable them.","Increase InitializationTimeout if the host is consistently slow.","Ensure the process is not starting many monitors concurrently (resource contention)."],"exampleFix":"// before\nmonitor.Start();\n\n// after\ntry\n{\n    monitor.Start();\n}\ncatch (TimeoutException)\n{\n    logger.LogWarning(\"RawInput 初始化超时，重试一次\");\n    Thread.Sleep(500);\n    monitor.Start();\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"async Task StartWithRetryAsync(RawInputMonitor m, ILogger log)\n{\n    for (int attempt = 0; attempt < 2; attempt++)\n    {\n        try { m.Start(); return; }\n        catch (TimeoutException ex)\n        {\n            log.LogWarning(\"RawInput 初始化超时 (尝试 {N})\", attempt + 1);\n            await Task.Delay(500);\n        }\n    }\n    throw;\n}","preventionTips":["Retry once after a short delay; transient init stalls often clear.","Avoid starting the monitor during peak CPU load.","Disable interfering security/overlay software that hooks thread creation."],"tags":["rawinput","mouse","timeout","sta-thread","monitor"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}