Posts

Showing posts from May, 2017

WPF Dispatcher - Accessing UI Thread From Another Thread

If you're a C#.NET developer creating a WPF application and it's obvious that you could come across a scenarios to use multi threading. Have you ever tried to access an element in the UI from another thread and come across an exception with the following message ? "The calling thread cannot access this object because a different thread owns it." What's the reason for such message ? It's something like this. In certain programming languages the application starts with something called the main thread or the UI thread. And usually, the elements of the UI, are created with that main thread. When another thread tries to access it, that message is given. Put into simple words, "When you're doing something to one of the UI elements, you have to do it through the UI thread." The next question is,  how to do such thing ? When you're working with WPF, every thread has an associated Dispatcher which provides