HandyOrg/HandyControl · error · InvalidOperationException

Calls to BeginInit cannot be nested.

Error message

Calls to BeginInit cannot be nested.

What it means

JumpList.BeginInit implements ISupportInitialize and may only be called when the JumpList is still in its unmodified state (_IsUnmodified). Calling BeginInit after the list has been changed — effectively nesting or re-initializing initialization — throws InvalidOperationException "Calls to BeginInit cannot be nested."

Solutions

  1. Ensure every BeginInit is matched by exactly one EndInit before calling BeginInit again.
  2. Do not call BeginInit after mutating the list — mutate inside the BeginInit/EndInit pair, or create a fresh JumpList instance instead.
  3. Guard with a flag: only call BeginInit if you did not already initialize this instance.
  4. If re-initialization is needed, construct a new JumpList, populate it, and call SetJumpList + Apply again.

Example fix

// before
jumpList.BeginInit();
jumpList.JumpItems.Add(task);
jumpList.BeginInit(); // throws: nested

// after
jumpList.BeginInit();
jumpList.JumpItems.Add(task);
jumpList.EndInit(); // complete the cycle before any further init
Defensive patterns

Strategy: validation

Validate before calling

if (_initStarted) return; // BeginInit already in progress or list already modified
jumpList.BeginInit();

Try / catch

try { jumpList.BeginInit(); /* populate */ jumpList.EndInit(); }
catch (InvalidOperationException ex) { Log.Error(ex.Message); }

Prevention

When it happens

Trigger: Calling BeginInit() twice without an intervening EndInit(); calling BeginInit() after adding JumpTasks/JumpPaths or otherwise mutating the JumpList; XAML + code both initializing the same JumpList instance.

Common situations: Re-applying a jump list on shutdown after it was already initialized; component designer/initialization frameworks invoking ISupportInitialize twice; manual BeginInit around code that itself triggers initialization.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/eb9eb10160dbd435. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/Microsoft.Windows.Shell/JumpList.cs:128

        {
            return this._jumpItems;
        }
    }

    private bool _IsUnmodified
    {
        get
        {
            return this._initializing == null && this.JumpItems.Count == 0 && !this.ShowRecentCategory && !this.ShowFrequentCategory;
        }
    }

    [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "BeginInit")]
    public void BeginInit()
    {
        if (!this._IsUnmodified)
        {
            throw new InvalidOperationException("Calls to BeginInit cannot be nested.");
        }
        this._initializing = new bool?(true);
    }

    [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "EndInit")]
    [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "BeginInit")]
    public void EndInit()
    {
        if (this._initializing != true)
        {
            throw new NotSupportedException("Can't call EndInit without first calling BeginInit.");
        }
        this._initializing = new bool?(false);
        this.ApplyFromApplication();
    }

    private static string _RuntimeId
    {

View on GitHub (pinned to 2c0875ebd6)