{"record":{"id":"a39d174a44a30496","repo":"dotnet/wpf","slug":"sr-generationnotinprogress","errorCode":null,"errorMessage":"SR.GenerationNotInProgress","messagePattern":"SR\\.GenerationNotInProgress","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs","lineNumber":225,"sourceCode":"                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);\n\n            return _generator.GenerateNext(true, out isNewlyRealized);\n        }\n\n        DependencyObject IItemContainerGenerator.GenerateNext(out bool isNewlyRealized)\n        {\n            if (_generator == null)\n                throw new InvalidOperationException(SR.GenerationNotInProgress);\n\n            return _generator.GenerateNext(false, out isNewlyRealized);\n        }\n\n        /// <summary>\n        /// Prepare the given element to act as the container for the\n        /// corresponding item.  This includes applying the container style,\n        /// forwarding information from the host control (ItemTemplate, etc.),\n        /// and other small adjustments.\n        /// </summary>","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs#L207-L243","documentation":"IItemContainerGenerator.GenerateNext() throws InvalidOperationException when the generator has no active generation session. GenerateNext may only be called between StartAt and a call to GenerateNext/Remove that ends the session; when the internal _generator worker is null, the generator is not in progress. The library throws to prevent generating containers outside a valid generation lifecycle.","triggerScenarios":"Calling ((IItemContainerGenerator)itemsControl.ItemContainerGenerator).GenerateNext() without first calling StartAt (or after the generation session was ended, e.g. after Remove/Recycle or a completed pass).","commonSituations":"Custom panels or virtualizing controls that implement IItemContainerGenerator directly but forget to call StartAt; re-entrant calls during layout after the previous generation pass already closed the session.","solutions":["Call StartAt(generatorPosition, GeneratorDirection.Forward, true) before the first GenerateNext call.","Track the session state: only call GenerateNext while your generation loop (between StartAt and RemoveAllInternal/Recycle) is active.","Use ItemsControl/ItemContainerGenerator public APIs (GenerateChild, ContainerForIndex) instead of driving IItemContainerGenerator manually."],"exampleFix":"// before\nvar container = generator.GenerateNext();\n\n// after\nvar pos = new GeneratorPosition(-1, 0);\nusing (generator.StartAt(pos, GeneratorDirection.Forward, true))\n{\n    bool isNewlyRealized;\n    var container = generator.GenerateNext(out isNewlyRealized);\n    generator.PrepareItemContainer(container);\n}","handlingStrategy":"validation","validationCode":"// Track generation session state yourself\nbool isGenerating = false;\ngenerator.StartAt(new GeneratorPosition(-1, 0), GeneratorDirection.Forward, true);\nisGenerating = true;\nif (!isGenerating) throw new InvalidOperationException(\"Call StartAt first\");\nvar c = generator.GenerateNext(out bool realized);","typeGuard":"bool CanGenerate(IItemContainerGenerator g, bool sessionStarted) => sessionStarted;","tryCatchPattern":null,"preventionTips":["Always pair StartAt with your GenerateNext loop in the same method/scope.","Never cache generator session state across layout passes.","Prefer deriving from VirtualizingPanel which manages the session for you."],"tags":["wpf","itemcontainergenerator","invalid-operation","virtualization"],"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"}