The plug-in Framework Discovery series uses multiple UI threads to promote performance

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.