C # Learning Five---wpf-ui threads

Source: Internet
Author: User

Write this record is because in today's blog, search for information on C #, see a lot of God of God, ashamed oh, they write is to write learning steps, no technical content, but as their own learning notes, I would like to write down ... Come on...

In Hego predecessors of the works of C#--WPF, I read some today, very useful, this is his C #-WPF Web site: http://www.cnblogs.com/hegezhou_hot/category/260429.html

Everyone can go to his study, I only record my own today's study:


I used my last C # Learning II---Create a simple graphics application-WPF to do the experiment, add a button inside, renamed UI;

As shown in figure:



The code is as follows:


        private void Textui_click (object sender, RoutedEventArgs e)
        {
            thread t = new Thread (delegate ()
            {
                This.userName.Text = "Test UI";

            };
            T.start ();
        }

The following error occurred while running:


UI controls in WPF, if we look at nature, they all inherit from Dispatcherobject, so they must be scheduled and used by the UI thread, and if we manipulate interface-related elements in other background threads, the following exception message appears: (This is copied)

We change the procedure as follows:

        private void Textui_click (object sender, RoutedEventArgs e)
        {
            thread t = new Thread (delegate ()
            {
                // This.userName.Text = "Test UI";
                This.userName.Dispatcher.Invoke (New Action (delegate () {Username.text = "Test UI";}));
            T.start ();
        }

Remember to add a sentence at the beginning:


The following is the results of the operation, we first click on the UI, and then click OK, the result is as shown:


We need to know that the UI thread will eventually execute, and that we need special attention when we do multithreaded programming in WPF. In the process of MVVM programming, we need to pay attention to this problem when we modify the binding source object in our team ViewModel. (This is copied over)


Thanks to Hego, this concludes today.

OVER!!!



Related Article

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.