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
Is Using an an 'Async' Lambda with 'Task.Run()' Redundant
How to Hide Public Methods from Intellisense
Return All Enumerables with Yield Return at Once; Without Looping Through
Foo.Cmd Won't Output Lines in Process (On Website)
C# MACro Definitions in Preprocessor
Why Gettype Returns System.Int32 Instead of Nullable<Int32>
No Access to the Session Information Through Signalr Hub. Is My Design Is Wrong
Wpf Binding Not Updating the View
How to Segment the Elements Iterated Over in a Foreach Loop
Sorting a Dictionary in Place with Respect to Keys
Newtonsoft.JSON Assembly Package Version Mismatch
Extension Method on Enumeration, Not Instance of Enumeration
Regex That Matches a Newline (\N) in C#
No Itemchecked Event in a Checkedlistbox
How to Call a SQL Stored Procedure Using Entityframework 7 and ASP.NET 5