Dirkster99 / AvalonDock

Our own development branch of the well known WPF document docking library
Microsoft Public License
1.41k stars 321 forks source link

InvalidOperationException in Multi-Threading environment #395

Open arthachitra opened 2 years ago

arthachitra commented 2 years ago

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.