protected virtual void LoadThreadSeparatedControl() { if (SeparateThreadDispatcher != null) { return; } AutoResetEvent sync = new AutoResetEvent(false); HostVisual = new HostVisual(); AddLogicalChild(HostVisual); AddVisualChild(HostVisual); if (DesignerProperties.GetIsInDesignMode(this)) { return; } var thread = new Thread(() => { TargetElement = CreateThreadSeparatedControl(); if (TargetElement == null) { return; } VisualTarget = new VisualTargetPresentationSource(HostVisual); VisualTarget.RootVisual = TargetElement; Dispatcher.BeginInvoke(new Action(() => { InvalidateMeasure(); })); sync.Set(); Dispatcher.Run(); VisualTarget.Dispose(); }) { IsBackground = true }; thread.SetApartmentState(ApartmentState.STA); thread.Start(); sync.WaitOne(); }
protected override void LoadThreadSeparatedControl() { HostVisual = new HostVisual(); AddLogicalChild(HostVisual); AddVisualChild(HostVisual); if (DesignerProperties.GetIsInDesignMode(this)) { return; } CommonDispatcher.BeginInvoke(new Action(() => { lock (_lock) { if (HostVisual == null) { return; // can happen if control was loaded then immediately unloaded } if (TargetElement == null) { TargetElement = CreateThreadSeparatedControl(); } if (TargetElement == null) { return; } VisualTarget = new VisualTargetPresentationSource(HostVisual); VisualTarget.RootVisual = TargetElement; } Dispatcher.BeginInvoke(new Action(() => { InvalidateMeasure(); })); })); }