AvalonDock icon indicating copy to clipboard operation
AvalonDock copied to clipboard

InvalidOperationException in Multi-Threading environment

Open arthachitra opened this issue 2 years ago • 0 comments

Hi, I am getting the below exception while using the control in a multi-threading environment.

System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it control

Adding an Invoke however resolved the scenario. Please find the changes I incorporated:

In DockingManager.cs file

private static void OnContextMenuPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			if (e.NewValue is ContextMenu contextMenu)
			{
				contextMenu.Dispatcher.Invoke(() =>
				{
					contextMenu.Resources = ((DockingManager)d).Resources;
				}, DispatcherPriority.DataBind);
			}
		}

In FocusElementManager.cs file

private static void WindowFocusChanging(object sender, FocusChangeEventArgs e)
		{
			foreach (var manager in _managers)
			{
				var hostContainingFocusedHandle = manager.Dispatcher.Invoke(() => manager.FindLogicalChildren<HwndHost>().FirstOrDefault(hw => Win32Helper.IsChild(hw.Handle, e.GotFocusWinHandle)), DispatcherPriority.DataBind);

Would be great if you update the source at your end too.

Thanks for the wonderful control.

arthachitra avatar Nov 03 '22 09:11 arthachitra