[ACCEPTED]-Dispatcher to Thread relationships in WPF-dispatcher
WPF application has 2 threads (one for 19 input, the other for UI)
This statement is 18 not entirely correct. A WPF application 17 has only one UI thread that handles all 16 the UI interaction and user input. There 15 is also a "hidden" thread responsible for 14 rendering, but normally developers don't 13 deal with it.
Dispatcher / Thread relationship 12 is one to one, i.e. one Dispatcher is always 11 assoticated with one thread and can be used 10 to dispatch execution to that thread. Dispatcher.CurrentDispatcher
returns 9 the dispatcher for the current thread, that 8 is, when you call Dispatcher.CurrentDispatcher
on a worker thread you 7 get a dispatcher for that working thread.
Dispatchers 6 are created on demand, which means if you 5 access Dispatcher.CurrentDispatcher
and there is no dispatcher associated 4 with the current thread, one will be created.
That 3 being said, the number of dispatchers in 2 the application is always less or equal 1 to the number of threads in the application.
WPF application by default has only one 22 Dispatcher. The dispatcher is the only thread 21 that will allow you to interact with UI 20 elements. It abstracts implementations from 19 you, so you only need to worry about being 18 on the UI thread ie the Dispatcher.
If you 17 are trying to directly interact with a visual 16 (eg, set a text on a text box using txtBkx.Text = "new"
), from 15 a worker thread, then you will have to switch 14 to a UI thread:
Application.Current.Dispatcher.Invoke(
() => { txtBkx.Text = "new"; });
Alternatively you can use 13 SynchronizationContext.Current
(while on a UI thread) and use that to 12 execute delegates on a UI thread from a 11 different thread. As you should note that 10 Dispatcher.CurrentDispatcher
may not always be set.
Now you can in fact 9 create different WPF windows in the same 8 application and have an individual dispatcher 7 for each window:
Thread thread = new Thread(() =>
{
Window1 w = new Window1();
w.Show();
w.Closed += (sender2, e2) =>
w.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
As a side note remember 6 in MVVM, you can update model from a non 5 UI thread and raise property changed events 4 from a non UI thread, as WPF will marshal 3 PropertyChanged events for you. Raising 2 CollectionChanged has to be on a UI thread 1 though.
A dispatcher is always associated with a thread and 10 a thread can have at most one dispatcher 9 running at the same time. A thread does 8 not need to have a dispatcher.
By default 7 there is only one Dispatcher - For the UI. Sometimes 6 it makes sense to have other dispatchers, other 5 time it does not. A dispatching thread needs 4 to block in the Dispatcher.Run()
method in order to process 3 invokes to the dispatcher. A thread such 2 as your console input thread will not be 1 availible to process invokes.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.