Update Requires a Valid Updatecommand When Passed Datarow Collection with Modified Rows

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

This is a common problem with people getting started. Whenever you update your UI elements from a thread other than the main thread, you need to use:

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

You can also use control.Dispatcher.CheckAccess() to check whether the current thread owns the control. If it does own it, your code looks as normal. Otherwise, use above pattern.

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

Try using

Application.Current.Dispatcher.Invoke

instead of

(MainWindow)System.Windows.Application.Current.MainWindow).pConnect.lbLine.Dispatcher.Invoke

The problem may be that the pConnect or lbLine is a UI object, so it, like any other UI object, cannot be used from other threads than the main thread.

Usually there is only one UI/Main thread, so all dispatchers or other ways to move execution to it will be equivalent.

The calling thread cannot access this object because a different thread owns it error in C# WPF Application?

You should use :

this.Dispatcher.Invoke((Action)(() =>
{
...// your code refresh listbox Items
}));

please have a look at : The calling thread cannot access this object because a different thread owns it

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

Both the DrawingImage and the DrawingGroup inherit from DispatcherObject, which means that they need to be accessed from the thread on which they were created. That is why your version where all of the work is invoked back to the dispatcher works correctly.

As pointed out by Brian Reichle, these object also inherit from System.Windows.Freezable, which you can leverage to allow cross thread access to the objects.



Related Topics



Leave a reply



Submit