All users who know about the WPF thread model know that the UI thread is responsible for rendering and managing the UI, while the UI element (derived from
Dispatcherobject) can only be accessed by the thread that creates this element, which causes some time-consuming UI operations
The entire applicationProgramPerformance, unresponsiveness, and long waits sometimes make people crazy, And the UI thread once became an indispensable
The wider the gap.
For the framework, the behavior of a plug-in should not affect the stability of other plug-ins and the entire platform.
Running WPF application with multiple UI threads and dispatcherobject and WPF line
Cheng ModelArticleAfter that, the idea was opened immediately. The previous article described how to use multiple UI lines in the WPF application.
If each independent plug-in is in a different UI thread, the natural performance will be improved.
The message loop of Win32 and the thread model of WPF are analyzed thoroughly.
The following describes how to create a new UI thread.
Thread thread = new thread () => {window win = new window {Title = string. format ("thread ID: {0}", thread. currentthread. managedthreadid)}; win. show (); dispatcher. run () ;}); thread. isbackground = true; thread. setapartmentstate (apartmentstate. sta); thread. start ();
In this way, the window will run in the new thread, dispatcher. Run () will bring the current thread into the message loop, and set the isbackground of the thread to true to ensure that the program is exited.
The thread is automatically recycled when it is released; otherwise, the process will reside.