Timely UI updates in WPF

Source: Internet
Author: User

Whether in winform or WPF, we need to make a progress bar for a large loop or time-consuming processing. The first thing we think of is multithreading. Otherwise, the progress bar will be suspended and will not be updated. Multithreading is troublesome. If you just update the UI, you can use systems. doevents.

In winform, the message queue is processed using peekmessage, which gives the UI the opportunity to update. In WPF, pushframe can be used in dispatch to achieve the same effect.

Code:
  1. Code:
  2. Public void doevents ()
  3. {
  4. Dispatcherframe frame = new dispatcherframe ();
  5. Dispatcher. currentdispatcher. begininvoke (dispatcherpriority. background,
  6. New dispatcheroperationcallback (delegate (Object F)
  7. {
  8. (Dispatcherframe) f). Continue = false;
  9. Return NULL;
  10. }
  11. ), Frame );
  12. Dispatcher. pushframe (FRAME );
  13. }

After writing this method, we can call doevents () after the UI to be updated in a loop or event.

 

 

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.