{"record":{"id":"7a2e4326a02f03a9","repo":"dotnet/wpf","slug":"visualtarget-anothertargetalreadyconnected","errorCode":null,"errorMessage":"VisualTarget_AnotherTargetAlreadyConnected","messagePattern":"VisualTarget_AnotherTargetAlreadyConnected","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/HostVisual.cs","lineNumber":145,"sourceCode":"        /// </summary>\n        internal void BeginHosting(VisualTarget target)\n        {\n            //\n            // This method is executed on the visual target thread.\n            //\n\n            Debug.Assert(target != null);\n            Debug.Assert(target.Dispatcher.Thread == Thread.CurrentThread);\n\n            using (CompositionEngineLock.Acquire())\n            {\n                //\n                // Check if another target is already hosted by this\n                // visual and throw exception if this is the case.\n                //\n                if (_target != null)\n                {\n                    throw new InvalidOperationException(\n                        SR.VisualTarget_AnotherTargetAlreadyConnected\n                        );\n                }\n\n                _target = target;\n\n                //\n                // If HostVisual and VisualTarget on same thread, then call Invalidate\n                // directly. Otherwise post invalidate message to the host visual thread\n                // indicating that content update is required.\n                //\n                if (this.CheckAccess())\n                {\n                    Invalidate();\n                }\n                else\n                {\n                    Dispatcher.BeginInvoke(","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/HostVisual.cs#L127-L163","documentation":"HostVisual.BeginHosting throws InvalidOperationException with 'VisualTarget_AnotherTargetAlreadyConnected' when the HostVisual already has a composition target attached. A HostVisual can only host one VisualTarget for its lifetime; re-hosting is an invalid state transition.","triggerScenarios":"Creating a second VisualTarget and calling BeginHosting with the same HostVisual instance that already has _target set (e.g. re-creating a VisualTarget on the same HostVisual after a render-thread reset).","commonSituations":"Multi-threaded UI scenarios that swap render targets dynamically (common in HwndSource/VisualTarget cross-thread rendering), where a new VisualTarget is created per frame or per device and reused with one HostVisual.","solutions":["Create a new HostVisual for each VisualTarget connection","Detach/dispose the old VisualTarget and HostVisual before hosting a new target","Track whether the HostVisual is already connected and reuse the existing VisualTarget"],"exampleFix":"// before\nhostVisual.BeginHosting(new VisualTarget(hostVisual)); // second call\n// after\nif (hostVisual.Target == null)\n{\n    hostVisual.BeginHosting(new VisualTarget(hostVisual));\n}\nelse\n{\n    hostVisual = new HostVisual();\n    hostVisual.BeginHosting(new VisualTarget(hostVisual));\n}","handlingStrategy":"validation","validationCode":"if (hostVisual.Target != null)\n    throw new InvalidOperationException(\"HostVisual already connected; create a new HostVisual.\");","typeGuard":"bool CanHost(HostVisual hv) => hv != null && hv.Target == null;","tryCatchPattern":"try { hostVisual.BeginHosting(target); }\ncatch (InvalidOperationException) { hostVisual = new HostVisual(); hostVisual.BeginHosting(target); }","preventionTips":["One VisualTarget per HostVisual for its lifetime","Recreate the HostVisual when swapping render targets","Track connection state to avoid double-hosting in retry/frame loops"],"tags":["wpf","visual-target","invalid-operation","state-conflict"],"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-22T01:17:13.364Z"}