Syed
Syed

Reputation: 931

How Dispatcher.BeginInvoke(...) updates the UI controls in WPF?

I read that Dispatcher.BeginInvoke() will run in a separate thread from ThreadPool.

Is it correct?

If it is correct then my question is: If it running is separate thread which is not UI thread, how it will update the UI?

Upvotes: 0

Views: 824

Answers (2)

srgstm
srgstm

Reputation: 3759

In ordinary WPF application all UI objects are associated with a single dispatcher and the dispatcher is associated with a single thread. BeginInvoke asynchronously runs a specified delegate on the thread associated with the dispatcher (in the case of a UI dispatcher it will be the UI thread). You only need to call BeginInvoke (or the Invoke which is same as BeginInvoke but blocking) from a thread other than the UI thread.

If you want to update UI from a code running in a ThreadPool's thread, get the reference to the UI dispatcher and call BeginInvoke or Invoke and it will transfer the call to the UI thread.

Upvotes: 1

Richard Szalay
Richard Szalay

Reputation: 84754

Dispatcher.BeginInvoke schedules an action to be called on the UI thread and is called from a background thread to update UI elements.

Upvotes: 2

Related Questions