{"record":{"id":"157297b84287dc74","repo":"stride3d/stride","slug":"unable-to-initialize-the-window-manager","errorCode":null,"errorMessage":"Unable to initialize the window manager.","messagePattern":"Unable to initialize the window manager\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Windows/WindowManager.cs","lineNumber":47,"sourceCode":"        private static IntPtr hook;\n        private static Dispatcher dispatcher;\n        private static bool initialized;\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"WindowManager\"/> class.\n        /// </summary>\n        public WindowManager([NotNull] Dispatcher dispatcher)\n        {\n            if (dispatcher == null) throw new ArgumentNullException(nameof(dispatcher));\n            if (initialized) throw new InvalidOperationException(\"An instance of WindowManager is already existing.\");\n\n            initialized = true;\n            winEventProc = WinEventProc;\n            WindowManager.dispatcher = dispatcher;\n            uint processId = (uint)Process.GetCurrentProcess().Id;\n            hook = NativeHelper.SetWinEventHook(NativeHelper.EVENT_OBJECT_SHOW, NativeHelper.EVENT_OBJECT_HIDE, IntPtr.Zero, winEventProc, processId, 0, NativeHelper.WINEVENT_OUTOFCONTEXT);\n            if (hook == IntPtr.Zero)\n                throw new InvalidOperationException(\"Unable to initialize the window manager.\");\n\n            Logger.Info($\"{nameof(WindowManager)} initialized\");\n        }\n\n#if DEBUG // Use a logger result for debugging\n        public static Logger Logger { get; } = new LoggerResult();\n#else\n        public static Logger Logger { get; } = GlobalLogger.GetLogger(nameof(WindowManager));\n#endif\n\n        /// <summary>\n        /// Gets the current main window.\n        /// </summary>\n        public static WindowInfo MainWindow { get; private set; }\n\n        /// <summary>\n        /// Gets the collection of currently visible modal windows.\n        /// </summary>","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Windows/WindowManager.cs#L29-L65","documentation":"WindowManager installs a WinEvent hook via NativeHelper.SetWinEventHook to watch for window show/hide events. If the native call returns IntPtr.Zero the hook was not installed and the constructor throws InvalidOperationException, failing fast rather than running a manager that will never see window events.","triggerScenarios":"SetWinEventHook failing at construction — typically because the calling thread is not running a message pump, an out-of-resources/desktop situation, or running in an environment where WinEvents are unavailable (e.g. certain services or non-interactive sessions).","commonSituations":"Constructing WindowManager on a background thread without a message loop; running inside a Windows Service or session 0 where UI hooks are restricted; restricted desktop or security software blocking hooks.","solutions":["Construct WindowManager on the UI/dispatcher thread that pumps messages","Verify the process runs in an interactive desktop session (not a service/session 0)","Catch the exception and degrade gracefully if window tracking is optional; check NativeHelper constants and the WinEvent parameters are correct"],"exampleFix":"// before\nTask.Run(() => new WindowManager(dispatcher)); // no message pump\n// after\ndispatcher.Invoke(() => { manager = new WindowManager(dispatcher); });","handlingStrategy":"try-catch","validationCode":"if (Environment.UserInteractive == false) skipWindowManagerInit();","typeGuard":null,"tryCatchPattern":"try { manager = new WindowManager(dispatcher); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Unable to initialize\")) { /* run without window tracking */ }","preventionTips":["Construct on a message-pumping (UI) thread","Don't use in services/non-interactive sessions","Make window tracking optional with graceful degradation"],"tags":["wpf","native-interop","winevent-hook"],"backgroundTag":"module-init-failed","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}