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.