Calling thread cannot access object when it's created from the same thread

a--

I'm dynamically building my WPF layout and for some reason, when I try to add a control to a grid using a different thread, I'm getting:

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

I know that I need to be invoking my controls if they are being access from different threads and this is what I'm doing.

public void controlInvoke(Grid control, Action action)
{
    control.Dispatcher.Invoke((Delegate)action);
}

When I add a new row definition to my grid from the thread, it works fine.

controlInvoke(installedApplicationGrid, () =>
{
    installedApplicationGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
});  

The children are built from another method, which is below.

private StackPanel BuildAppPanel()
{
    StackPanel panel = new StackPanel();

    Grid innerGrid = new Grid();
    innerGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50, GridUnitType.Star) });
    innerGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50, GridUnitType.Star) });
    innerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100, GridUnitType.Star) });

    panel.Children.Add(innerGrid);

    return panel;
}

The actual code that generates the panel, and tries to add it to my Grid is.

StackPanel applicationPanel = BuildAppPanel();

controlInvoke(installedApplicationGrid, () =>
{
    installedApplicationGrid.Children.Add(applicationPanel);
});

It is at this point, when the error message shows. What I don't understand is why this is happening when all of the controls are being dynamically built using this thread. It doesn't make sense to me why adding the row definitions is fine, but adding a new control is not.

Could someone shed some light on this?

a--

So after looking at this a little more carefully, I fixed it by moving the BuildPanel inside of the action.

controlInvoke(installedApplicationGrid, () =>
{
    StackPanel applicationPanel = BuildAppPanel();
    installedApplicationGrid.Children.Add(applicationPanel);
});     

All is working now. Thanks @Fildor for the suggestion.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

From Dev

The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

From

The calling thread cannot access this object because a different thread owns it

From Dev

Calling thread cannot access object due to separate thread ownership

From Dev

the calling thread cannot access this object because a different thread owns it wpf

From Dev

The Calling thread cannot access this object because a different thread owns it,WPF

From Dev

WPF: The calling thread cannot access this object because a different thread owns it

From Dev

The calling thread cannot access this object because a different thread owns it (with Dispatcher)

From Java

Calling a method from the class that created a thread, in the thread

From Dev

"calling thread cannot access this object" exception when working with live visual tree in multithreaded UI WPF app

From Dev

c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

From Dev

Famous the calling thread cannot access this object because a different issue

From Dev

ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

From Dev

The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

From Dev

Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

From Dev

Is dealloc guaranteed to be called on the same thread that created the object?

From Dev

The thread cannot access an object because a different thread owns it when using wpf

From Dev

Is it normal for COM RWC's created in STA thread to disconnect from the underlying COM object on thread termination?

From Dev

Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

From Dev

destroy thread's object from a joinable thread

From Dev

Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

From Dev

The calling thread cannot access this object even after adding Dispatcher.Invoke

From Dev

WPF error 'calling thread cannot access this object' trying to play sounds in c# class

From Java

Main thread is blocked when calling method from another thread

From Dev

Why is GUI thread blocked from worker thread when calling waitForReadyRead?

From Dev

Detect the thread that created an object

From Dev

ConcurrentModificationException when multiple thread access the same Collection

From Java

new Thread created when calling SwingUtilities.invokeAndWait()?

From Dev

Must an object's ctor and dtor be on the same thread?

Related Related

  1. 1

    "The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

  2. 2

    The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

  3. 3

    The calling thread cannot access this object because a different thread owns it

  4. 4

    Calling thread cannot access object due to separate thread ownership

  5. 5

    the calling thread cannot access this object because a different thread owns it wpf

  6. 6

    The Calling thread cannot access this object because a different thread owns it,WPF

  7. 7

    WPF: The calling thread cannot access this object because a different thread owns it

  8. 8

    The calling thread cannot access this object because a different thread owns it (with Dispatcher)

  9. 9

    Calling a method from the class that created a thread, in the thread

  10. 10

    "calling thread cannot access this object" exception when working with live visual tree in multithreaded UI WPF app

  11. 11

    c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

  12. 12

    Famous the calling thread cannot access this object because a different issue

  13. 13

    ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

  14. 14

    The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

  15. 15

    Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

  16. 16

    Is dealloc guaranteed to be called on the same thread that created the object?

  17. 17

    The thread cannot access an object because a different thread owns it when using wpf

  18. 18

    Is it normal for COM RWC's created in STA thread to disconnect from the underlying COM object on thread termination?

  19. 19

    Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

  20. 20

    destroy thread's object from a joinable thread

  21. 21

    Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

  22. 22

    The calling thread cannot access this object even after adding Dispatcher.Invoke

  23. 23

    WPF error 'calling thread cannot access this object' trying to play sounds in c# class

  24. 24

    Main thread is blocked when calling method from another thread

  25. 25

    Why is GUI thread blocked from worker thread when calling waitForReadyRead?

  26. 26

    Detect the thread that created an object

  27. 27

    ConcurrentModificationException when multiple thread access the same Collection

  28. 28

    new Thread created when calling SwingUtilities.invokeAndWait()?

  29. 29

    Must an object's ctor and dtor be on the same thread?

HotTag

Archive