What does invoke do?

Source: Internet
Author: User

Let's take a look at the following:Code

While (Progressbartest. value < 100 )
{
System. Threading. thread. Sleep ( 100 );
// Dispatcher. Invoke (new action () =>
This . Progressbartest. Value + = 1 ;
// }), System. Windows. Threading. dispatcherpriority. Background );
}

 

When the UI thread is busy, the UI will be locked and the progressbar will not be updated. After the while loop ends, it will be updated to 100. If the two lines of the comment are removed, the progress bar will be updated continuously as expected.

Let's look at this code again.

While (Progressbartest. value < 100 )
{
System. Threading. thread. Sleep ( 100 );
This . Progressbartest. Value + = 1 ;
Dispatcher. Invoke ( New Action () =>{}), system. Windows. Threading. dispatcherpriority. Background );
}

 

During running, you will find that the results are the same. That is to say, the empty invoke delegate has the same effect. My idea is that when invoke is used, the UI thread will wait until the render thread ends render to continue running. If the UI thread does not return, that is, the current dispatcheroperation is not completed, other do calls will not be performed unless the displayed request is.

Another idea is:This. Progressbartest. Value + =1This line of code actually calls a begininvoke. We look forward to some time for verification.

 

 

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.