{"record":{"id":"a10d655aba8c906a","repo":"dotnet/wpf","slug":"sr-generationinprogress","errorCode":null,"errorMessage":"SR.GenerationInProgress","messagePattern":"SR\\.GenerationInProgress","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs","lineNumber":207,"sourceCode":"        /// Error, as appropriate.\n        /// </remarks>\n        IDisposable IItemContainerGenerator.StartAt(GeneratorPosition position, GeneratorDirection direction)\n        {\n            return ((IItemContainerGenerator)this).StartAt(position, direction, false);\n        }\n\n        /// <summary> Begin generating at the given position and direction </summary>\n        /// <remarks>\n        /// This method must be called before calling GenerateNext.  It returns an\n        /// IDisposable object that tracks the lifetime of the generation loop.\n        /// This method sets the generator's status to GeneratingContent;  when\n        /// the IDisposable is disposed, the status changes to ContentReady or\n        /// Error, as appropriate.\n        /// </remarks>\n        IDisposable IItemContainerGenerator.StartAt(GeneratorPosition position, GeneratorDirection direction, bool allowStartAtRealizedItem)\n        {\n            if (_generator != null)\n                throw new InvalidOperationException(SR.GenerationInProgress);\n\n            _generator = new Generator(this, position, direction, allowStartAtRealizedItem);\n            return _generator;\n        }\n\n        public IDisposable GenerateBatches()\n        {\n            if (_isGeneratingBatches)\n                throw new InvalidOperationException(SR.GenerationInProgress);\n\n            return new BatchGenerator(this);\n        }\n\n        DependencyObject IItemContainerGenerator.GenerateNext()\n        {\n            bool isNewlyRealized;\n            if (_generator == null)\n                throw new InvalidOperationException(SR.GenerationNotInProgress);","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs#L189-L225","documentation":"IItemContainerGenerator.StartAt begins a generation pass by creating a Generator; only one generation may be active at a time. Calling StartAt while an existing generator is still active (_generator != null, i.e. its IDisposable not disposed) throws InvalidOperationException(SR.GenerationInProgress).","triggerScenarios":"Calling StartAt (or GenerateNext within it) without disposing the IDisposable returned by a previous StartAt; nested generation loops over the same generator.","commonSituations":"Custom virtualizing panels iterating items with a using-scope bug (early return or exception skipping Dispose); generating containers on two threads against the same generator.","solutions":["Dispose the IDisposable from each StartAt before calling StartAt again (use a using block)","Ensure exceptions inside generation loops don't leak undisposed generators","Avoid calling generator APIs from multiple threads; generation is single-pass, UI-thread work"],"exampleFix":"// before\nvar g1 = gen.StartAt(pos, dir, true);\nvar g2 = gen.StartAt(pos, dir, true); // throws\n// after\nusing (gen.StartAt(pos, dir, true))\n{\n    while (gen.GenerateNext() is DependencyObject c) { gen.Remove(c); }\n}","handlingStrategy":"validation","validationCode":"// track your own active generation token\nif (_activeGeneration == null) _activeGeneration = iicg.StartAt(pos, dir, allow);","typeGuard":null,"tryCatchPattern":"try { using (iicg.StartAt(pos, dir, allow)) { /* generate */ } } catch (InvalidOperationException ex) { /* a generation pass is still active */ }","preventionTips":["Always dispose the IDisposable from StartAt (use using)","Handle exceptions so Dispose still runs","Do not run generation concurrently on the same generator"],"tags":["wpf","itemcontainergenerator","generation"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}