WPF regularly refreshes the UI and WPF refreshes the UI.

Source: Internet
Author: User

WPF regularly refreshes the UI and WPF refreshes the UI.

Code:

Using nhib.pdf. criterion; using System. collections. generic; using System. collections. objectModel; using System. componentModel; using System. data; using System. linq; using System. text; using System. threading; using System. windows; using System. windows. controls; using System. windows. data; using System. windows. documents; using System. windows. input; using System. windows. media; using System. windows. media. imaging; using System. windows. navigation; using System. windows. shapes; using Visifire. charts; namespace SunCreate. combatPlatform. client {public partial class MainPage: UserControl {private System. timers. timer timerNotice = null; public MainPage () {InitializeComponent ();} private void MainPage_Loaded (object sender, RoutedEventArgs e) {# region notification announcement if (timerNotice = null) {BindNotice (); timerNotice = new System. timers. timer (); timerNotice. elapsed + = new System. timers. elapsedEventHandler (o, eea) =>{ BindNotice () ;}); timerNotice. interval = 60*1000; timerNotice. start () ;}# endregion} private void MainPage_SizeChanged (object sender, SizeChangedEventArgs e) {}# region binding notification announcement private void BindNotice () {System. threading. tasks. task. factory. startNew () => {try {int total = 0; TES_NOTICE info = new TES_NOTICE (); IList <TES_NOTICE> list = new List <TES_NOTICE> (); list = HI. get <INoticeService> (). getListPage (null, DateTime. minValue, DateTime. minValue, 1, 50, ref total); Dispatcher. invoke (new Action () => {noticeListView. itemsSource = list;});} catch {}}) ;}# endregion }}View Code

Note: Using BackgroundWorker in the System. Timers. Timer event is invalid, that is, the following code cannot refresh the interface normally:

Using nhib.pdf. criterion; using System. collections. generic; using System. collections. objectModel; using System. componentModel; using System. data; using System. linq; using System. text; using System. threading; using System. windows; using System. windows. controls; using System. windows. data; using System. windows. documents; using System. windows. input; using System. windows. media; using System. windows. media. imaging; using System. windows. navigation; using System. windows. shapes; using Visifire. charts; namespace SunCreate. combatPlatform. client {public partial class MainPage: UserControl {private System. timers. timer timerNotice = null; public MainPage () {InitializeComponent ();} private void MainPage_Loaded (object sender, RoutedEventArgs e) {# region notification announcement if (timerNotice = null) {BindNotice (); timerNotice = new System. timers. timer (); timerNotice. elapsed + = new System. timers. elapsedEventHandler (o, eea) =>{ BindNotice () ;}); timerNotice. interval = 60*1000; timerNotice. start () ;}# endregion} private void MainPage_SizeChanged (object sender, SizeChangedEventArgs e) {}# region binding notification announcement private void BindNotice () {PT_USER_INFO user = new PT_USER_INFO (); IList <TES_COMBAT_TASK> taskList = new List <TES_COMBAT_TASK> (); BackgroundWorker worker = new BackgroundWorker (); worker. doWork + = (s, e) =>{ user = HI. get <Cache. ICacheService> (). userCache. getCurrentUserInfo (); taskList = HI. get <ITaskService> (). getCombatTaskByUserIDUnfinished (user. ID. toString () ;}; worker. runWorkerCompleted + = (s, e) => {try {taskListView. itemsSource = taskList;} catch {}}; worker. runWorkerAsync () ;}# endregion }}View Code

You can also use the DispatcherTimer interface to refresh the interface. However, time-consuming operations cannot be executed in the DispatcherTimer event. Otherwise, the interface will be stuck, so time-consuming scheduled operations, such as database query, need to use another System. timers. timer, relatively troublesome.

 

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.