3

In this case, when an ArcGIS Pro project is opened, non map panes are active. I would like the map pane to be activated on the click of the button as proceeding code requires the MapView.Active to not be null. Inside the OnClick function, I am attempting to activate the map pane but it is not activated in time. It's activated later on which causes errors as the active map view returns null for code that runs after this.

internal class MyDockpane_ShowButton : Button
{
    internal static void activatePane()
    {
        var mapPane = FrameworkApplication.Panes.OfType<IMapPane>().First() as Pane;
        mapPane.Activate();
    }
    protected override void OnClick()
    {
        activatePane()
        MyDockpaneViewModel.Show();            
    }
}

I believe that I need to await the activatePane() function so I re-wrote the code as below:

internal class MyDockpane_ShowButton : Button
{
    internal static async Task activatePane()
    {
        await QueuedTask.Run(() =>
        {
            var mapPane = FrameworkApplication.Panes.OfType<IMapPane>().First() as Pane;
            mapPane.Activate();
        });
    }
    protected override async void OnClick()
    {
        await activatePane();
        MyDockpaneViewModel.Show();            
    }
}

With the new code, I get System.InvalidOperationException: "The calling thread cannot access this object because a different thread owns it". What am I doing wrong here?

1 Answer 1

2

Whenever you update your UI elements from a thread other than the main thread, you need to use:

this.Dispatcher.Invoke(() =>
{
    ...// your code here.
});

or, if this is not a window

Application.Current.Dispatcher.Invoke(MyMethod, DispatcherPriority.ContextIdle);

For more information see the SO post: https://stackoverflow.com/questions/9732709/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.