{"record":{"id":"03e998313f0a3fe7","repo":"HandyOrg/HandyControl","slug":"can-t-call-endinit-without-first-calling-begininit","errorCode":null,"errorMessage":"Can't call EndInit without first calling BeginInit.","messagePattern":"Can't call EndInit without first calling BeginInit\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Shared/Microsoft.Windows.Shell/JumpList.cs","lineNumber":139,"sourceCode":"    }\n\n    [SuppressMessage(\"Microsoft.Naming\", \"CA2204:Literals should be spelled correctly\", MessageId = \"BeginInit\")]\n    public void BeginInit()\n    {\n        if (!this._IsUnmodified)\n        {\n            throw new InvalidOperationException(\"Calls to BeginInit cannot be nested.\");\n        }\n        this._initializing = new bool?(true);\n    }\n\n    [SuppressMessage(\"Microsoft.Naming\", \"CA2204:Literals should be spelled correctly\", MessageId = \"EndInit\")]\n    [SuppressMessage(\"Microsoft.Naming\", \"CA2204:Literals should be spelled correctly\", MessageId = \"BeginInit\")]\n    public void EndInit()\n    {\n        if (this._initializing != true)\n        {\n            throw new NotSupportedException(\"Can't call EndInit without first calling BeginInit.\");\n        }\n        this._initializing = new bool?(false);\n        this.ApplyFromApplication();\n    }\n\n    private static string _RuntimeId\n    {\n        get\n        {\n            string result;\n            HRESULT hrLeft = NativeMethods.GetCurrentProcessExplicitAppUserModelID(out result);\n            if (hrLeft == HRESULT.E_FAIL)\n            {\n                hrLeft = HRESULT.S_OK;\n                result = null;\n            }\n            hrLeft.ThrowIfFailed();\n            return result;","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/Microsoft.Windows.Shell/JumpList.cs#L121-L157","documentation":"JumpList.EndInit finalizes ISupportInitialize by applying the list to the application (ApplyFromApplication), but only when _initializing is true, i.e. a matching BeginInit ran. Calling EndInit without BeginInit throws NotSupportedException \"Can't call EndInit without first calling BeginInit.\"","triggerScenarios":"Calling EndInit() on a JumpList that never had BeginInit() called; calling EndInit() twice; a code path where an exception occurred between BeginInit and EndInit and EndInit is retried.","commonSituations":"Conditional code that skips BeginInit but unconditionally calls EndInit; duplicate initialization helpers both invoking EndInit; exception in the population code causing mismatched init pairs on retry.","solutions":["Always pair the calls: wrap population in try/finally so EndInit only runs after a successful BeginInit and is not called twice.","Track a bool _initialized flag in your code and only call EndInit when true.","If the list was populated without the BeginInit/EndInit pattern, call JumpList.ApplyFromApplication-equivalent flow correctly: BeginInit, mutate, EndInit once.","Create a new JumpList instance if initialization state is uncertain, rather than re-calling EndInit."],"exampleFix":"// before\nif (items.Count == 0) return; // BeginInit skipped\njumpList.EndInit(); // throws: no BeginInit\n\n// after\njumpList.BeginInit();\ntry { foreach (var i in items) jumpList.JumpItems.Add(i); }\nfinally { jumpList.EndInit(); }","handlingStrategy":"validation","validationCode":"if (!beginInitCalled) return; // EndInit requires a prior BeginInit","typeGuard":null,"tryCatchPattern":"try { jumpList.EndInit(); }\ncatch (NotSupportedException ex) { Log.Error(ex.Message); /* BeginInit was never called */ }","preventionTips":["Always use try/finally around population so Begin/End pairs stay balanced.","Track your own bool flag and only call EndInit when BeginInit succeeded.","Call EndInit exactly once per BeginInit — never on retry paths after exceptions.","Centralize jump-list initialization in one helper to prevent mismatched pairs."],"tags":["not-supported","jump-list","initialization","lifecycle","wpf"],"backgroundTag":"invalid-state-transition","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}