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.