Knowing the WPF threading model knows that the UI thread is responsible for rendering and managing the UI, while UI elements (derived from dispatcherobject) can only be accessed by the thread that created the element, which causes some time-consuming UI operations to affect the entire application performance. Unresponsive and lengthy waits can sometimes be maddening, and UI threads have become a barrier that cannot be exceeded.
For the framework, the behavior of a plug-in should not affect the stability of other plug-ins and the entire platform, and later, after reading the Running WPF application with multiple UI Threads and the Dispatcherobject and WPF threading model After two articles, the mind suddenly opened, the previous article is about using multiple UI threads in a WPF application, and if each individual plug-in is in a different UI thread, the natural performance will improve, and then the Win32 message loop and the WPF threading model are thoroughly analyzed.
Here's 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();
So window will run in a new thread, Dispatcher.run () causes the current thread to enter the message loop, and the set thread's IsBackground is true to ensure that the thread is automatically recycled when the program exits, otherwise a process-resident problem occurs.