System.Windows.Threading.Dispatcher and WinForms?

David Schmitt picture David Schmitt · Nov 19, 2008 · Viewed 65.4k times · Source

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.

Answer

Martin Konicek picture Martin Konicek · Jul 21, 2009

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.