{"record":{"id":"84c02b482b79a0d8","repo":"tui-cs/Terminal.Gui","slug":"invoke","errorCode":null,"errorMessage":"Invoke","messagePattern":"Invoke","errorType":"exception","errorClass":"NotInitializedException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/App/ApplicationImpl.Run.cs","lineNumber":60,"sourceCode":"    #endregion Main Loop Iteration\n\n    #region Timeouts and Invoke\n\n    /// <inheritdoc/>\n    public ITimedEvents TimedEvents { get; }\n\n    /// <inheritdoc/>\n    public object AddTimeout (TimeSpan time, Func<bool> callback) => TimedEvents.Add (time, callback);\n\n    /// <inheritdoc/>\n    public bool RemoveTimeout (object token) => TimedEvents.Remove (token);\n\n    /// <inheritdoc/>\n    public void Invoke (Action<IApplication>? action)\n    {\n        if (!Initialized)\n        {\n            throw new NotInitializedException (nameof (Invoke));\n        }\n\n        // If we are already on the main UI thread\n        if (TopRunnableView is IRunnable { IsRunning: true } && MainThreadId == Thread.CurrentThread.ManagedThreadId)\n        {\n            action?.Invoke (this);\n\n            return;\n        }\n\n        TimedEvents.Add (TimeSpan.Zero,\n                         () =>\n                         {\n                             action?.Invoke (this);\n\n                             return false;\n                         });\n    }","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/App/ApplicationImpl.Run.cs#L42-L78","documentation":"Thrown as NotInitializedException(nameof(Invoke)) by the Invoke(Action<IApplication>?) overload when Initialized is false. Invoke is meant to marshal work onto the UI thread via the main loop's TimedEvents, which do not exist before Init. The exception message is simply 'Invoke' because it is constructed from the member name.","triggerScenarios":"Calling app.Invoke(action) before app.Init() has completed; calling Invoke from a background thread that started before initialization finished; using Invoke in a constructor or static initializer that runs before the app is ready.","commonSituations":"Background workers spawned at startup that race ahead of Init; unit tests that call Invoke without setting up an initialized application; refactoring that moved an Invoke call earlier in the lifecycle.","solutions":["Ensure app.Init() has run before any Invoke call: guard with 'if (app.Initialized) app.Invoke(...);' or defer the call until after Init.","Start background work from inside the Run loop or after the InitializedChanged event fires.","In tests, use the provided test harness that initializes the application before invoking."],"exampleFix":"// before\napp.Invoke (a => UpdateStatus ());\n\n// after\nif (app.Initialized)\n{\n    app.Invoke (a => UpdateStatus ());\n}","handlingStrategy":"validation","validationCode":"if (app.Initialized)\n{\n    app.Invoke (a => DoWork ());\n}","typeGuard":"static bool CanInvoke (IApplication app) => app.Initialized;","tryCatchPattern":null,"preventionTips":["Only call Invoke after Init has completed.","Start background work from inside the Run loop or after InitializedChanged.","Guard cross-thread calls with app.Initialized."],"tags":["threading","lifecycle","main-loop","initialization"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}