Does a System.Windows.Threading.Dispatcher
work on the UI-thread of a WinForms
application?
If yes, why? It is coming from WindowsBase.dll which seems to be a WPF
component.
If not, how can I invoke work units back onto the UI-thread? I've found Control.BeginInvoke()
, but it seems clumsy to create a control only to reference the originating thread.
WinForms won't be deprecated until Win32 is ... which could be quite sometime! WPF on the other hand has few direct dependencies on Win32 so could potentially form the basis of a "fresh start" UI layer on a future version of windows.
The . NET Framework has full support for running multiple threads at once.
You can use Dispatcher even in a WinForms app. If you are sure to be on a UI thread (e.g. in an button. Click handler), Dispatcher. CurrentDispatcher gives you the UI thread dispatcher that you can later use to dispatch from background threads to the UI thread as usual.
A Dispatcher is responsible for managing the work for a thread. The UI thread is the thread that renders the UI. The UI thread queues work items inside an object called a Dispatcher. The Dispatcher selects work items on a priority basis and runs each one to completion.
You can use Dispatcher
even in a WinForms app.
If you are sure to be on a UI thread (e.g. in an button.Click handler), Dispatcher.CurrentDispatcher
gives you the UI thread dispatcher that you can later use to dispatch from background threads to the UI thread as usual.
Dispatcher is a WPF component, not a WinForms component.
If you want to dispatch work items on the UI thread, then you would have to either use Control.BeginInvoke as you've already found, or react to ResetEvents/WaitObjects across threads.
Usually invoking work items on the UI thread is a bad thing unless it's a UI piece of work (ie. updating a control's content or something) in which case the Control.BeginInvoke() would be sufficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With