Sharing objects between BackgroundWorker and the main thread

I have a ListBox object in my main thread (WPF application). Why can't I access it in the BackgroundWorker thread. As far as I know, the stack is separate for each thread, but the heap is shared. Not a ListBox created on the heap. In that case, why is it unavailable. I tried passing the ListBox link as a parameter and tried to access its contents in BackgroundWorker. Is the concept of sharing objects between threads different from C ++?

+5
source share
1 answer

Like WinForms, WPF design is greatly simplified due to the fact that all user interface elements are accessible only from the streams that created them.

When you try to write a multi-threaded program, this “feature” may seem like a serious limitation. It is not possible to change even the simplest properties of a user interface element directly from a background thread. If so, how can we use background threads in general with WPF?

The answer is that we must transform our user interface actions into a foreground thread where work can be done without conflict. More strictly speaking, we must perform our actions in the context of the thread that created the element we are trying to change.

WPF , :

, , Dispatcher , .

+10

All Articles