Dispatcher to Thread relationships in WPF

前端 未结 3 561
温柔的废话
温柔的废话 2020-12-08 07:23

It is not entirely clear to me how many Dispatchers there are in an application and how they are related to, or referenced from Threads.

As I understand it, a WPF ap

3条回答
  •  失恋的感觉
    2020-12-08 08:10

    WPF application has 2 threads (one for input, the other for UI)

    This statement is not entirely correct. A WPF application has only one UI thread that handles all the UI interaction and user input. There is also a "hidden" thread responsible for rendering, but normally developers don't deal with it.

    Dispatcher / Thread relationship is one to one, i.e. one Dispatcher is always assoticated with one thread and can be used to dispatch execution to that thread. Dispatcher.CurrentDispatcher returns the dispatcher for the current thread, that is, when you call Dispatcher.CurrentDispatcher on a worker thread you get a dispatcher for that working thread.

    Dispatchers are created on demand, which means if you access Dispatcher.CurrentDispatcher and there is no dispatcher associated with the current thread, one will be created.

    That being said, the number of dispatchers in the application is always less or equal to the number of threads in the application.

提交回复
热议问题