tui-cs/Terminal.Gui · error · InvalidOperationException

The view is already initialized.

Error message

The view is already initialized.

What it means

BeginInit is part of the ISupportInitialize lifecycle. It throws if the view is already initialized (IsInitialized == true), preventing double-initialization which would corrupt adornment state, key bindings, and SubView trees. Once EndInit has run, BeginInit must not be called again.

Source

Thrown at Terminal.Gui/ViewBase/View.cs:266

    /// <remarks>
    ///     <para>
    ///         Views can opt-in to more sophisticated initialization by implementing overrides to
    ///         <see cref="ISupportInitialize.BeginInit"/> and <see cref="ISupportInitialize.EndInit"/> which will be called
    ///         when the <see cref="SuperView"/> is initialized.
    ///     </para>
    ///     <para>
    ///         If first-run-only initialization is preferred, overrides to <see cref="ISupportInitializeNotification"/> can
    ///         be implemented too, in which case the <see cref="ISupportInitialize"/> methods will only be called if
    ///         <see cref="ISupportInitializeNotification.IsInitialized"/> is <see langword="false"/>. This allows proper
    ///         <see cref="View"/> inheritance hierarchies to override base class layout code optimally by doing so only on
    ///         first run, instead of on every run.
    ///     </para>
    /// </remarks>
    public virtual void BeginInit ()
    {
        if (IsInitialized)
        {
            throw new InvalidOperationException ("The view is already initialized.");
        }
#if AUTO_CANFOCUS
        _oldCanFocus = CanFocus;
        _oldTabIndex = _tabIndex;
#endif

        BeginInitAdornments ();

        if (InternalSubViews.Count <= 0)
        {
            return;
        }

        foreach (View view in InternalSubViews)
        {
            if (!view.IsInitialized)
            {
                view.BeginInit ();

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Guard with 'if (!view.IsInitialized)' before calling BeginInit.
  2. Let app.Run handle initialization instead of calling BeginInit/EndInit manually.
  3. Create a fresh View instance for a new session rather than re-initializing an old one.

Example fix

// before
view.BeginInit();
view.EndInit();
view.BeginInit(); // throws
// after
if (!view.IsInitialized) view.BeginInit();
Defensive patterns

Strategy: validation

Validate before calling

if (!view.IsInitialized) view.BeginInit();

Type guard

static bool CanBeginInit (View v) => !v.IsInitialized;

Prevention

When it happens

Trigger: Calling view.BeginInit() twice; calling BeginInit after the application framework already ran the init cycle (app.Run auto-initializes the view tree); manually initializing a view that was already initialized by a parent's EndInit.

Common situations: Mixing manual ISupportInitialize calls with app.Run (which initializes the whole tree); reusing a view instance across sessions; custom Runnable that double-initializes.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/ae373534eaa7cc0e. Report an issue: GitHub.