{"record":{"id":"39a9fb350340ef33","repo":"unoplatform/uno","slug":"thread-has-already-started","errorCode":null,"errorMessage":"Thread has already started","messagePattern":"Thread has already started","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/SourceGenerators/System.Xaml/System.Xaml/XamlBackgroundReader.cs","lineNumber":106,"sourceCode":"\t\t\tdo_work = false;\n\t\t}\n\t\t\n\t\tpublic override bool Read ()\n\t\t{\n\t\t\tif (q.IsEmpty)\n\t\t\t\twait.WaitOne ();\n\t\t\treturn q.Reader.Read ();\n\t\t}\n\t\t\n\t\tpublic void StartThread ()\n\t\t{\n\t\t\tStartThread (\"XAML reader thread\"); // documented name\n\t\t}\n\t\t\n\t\tpublic void StartThread (string threadName)\n\t\t{\n\t\t\tif (thread != null)\n\t\t\t\tthrow new InvalidOperationException (\"Thread has already started\");\n\t\t\tthread = new Thread (new ParameterizedThreadStart (delegate {\n\t\t\t\twhile (do_work && r.Read ()) {\n\t\t\t\t\tq.Writer.WriteNode (r);\n\t\t\t\t\twait.Set ();\n\t\t\t\t}\n\t\t\t\tread_all_done = true;\n\t\t\t})) { Name = threadName };\n\t\t\tthread.Start ();\n\t\t}\n\t}\n}\n","sourceCodeStart":88,"sourceCodeEnd":118,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SourceGenerators/System.Xaml/System.Xaml/XamlBackgroundReader.cs#L88-L118","documentation":"Thrown by XamlBackgroundReader.StartThread when the background reader thread has already been launched (thread field is non-null). XamlBackgroundReader is a single-use component that spawns exactly one reader thread; calling StartThread twice would corrupt the node queue and duplicate processing.","triggerScenarios":"Calling StartThread() (or StartThread(name)) a second time on the same XamlBackgroundReader instance. This commonly happens when reader code is reused across passes or when a retry/error-recovery path re-invokes Start.","commonSituations":"XAML processing pipeline that attempts to restart a failed read. Caching and reusing an XamlBackgroundReader for multiple documents. Misconfigured reader lifecycle management that does not create a fresh reader per document.","solutions":["Create a new XamlBackgroundReader instance for each XAML document rather than reusing one.","Track whether StartThread has been called (or check the internal state) and guard the second call.","Restructure the pipeline so each document gets its own reader lifecycle."],"exampleFix":"// before\nreader.StartThread();\n// ... process ...\nreader.StartThread(); // throws\n\n// after\nreader.StartThread();\n// ... process ...\nreader = new XamlBackgroundReader(schemaContext, innerReader);\nreader.StartThread();","handlingStrategy":"validation","validationCode":"if (reader.ThreadStarted) throw new InvalidOperationException(\"already started\");\nreader.StartThread();\n// or simply create a new reader per document","typeGuard":null,"tryCatchPattern":"try { reader.StartThread(); }\ncatch (InvalidOperationException) { reader = new XamlBackgroundReader(sctx, inner); reader.StartThread(); }","preventionTips":["Create a new XamlBackgroundReader for each XAML document.","Never reuse a reader across documents — treat it as single-use."],"tags":["xaml","threading","invalid-operation","background-reader","uno-xaml"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}