This article is mainly for you to introduce the WPF implementation of the regular refresh UI interface features, with a certain reference value, interested in small partners can refer to
The example of this article for everyone to share the WPF timed Refresh UI interface display of the specific code for your reference, the specific content is as follows
Code:
Using nhibernate.criterion;using system;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 timernotic e = 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, ref total); Dispatcher.invoke (new Action () = {Noticelistview.itemssource = list; })); } catch {}}); } #endregion}}
Description: The use of BackgroundWorker in System.Timers.Timer events is not valid, that is, the following code does not refresh the interface normally:
Using nhibernate.criterion;using system;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 timernotic e = 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}}
The
can also use the DispatcherTimer refresh interface, but time-consuming operations cannot be performed in DispatcherTimer events, otherwise the interface will be stuck, and then time-consuming timing operations, such as querying the database, require a System.Timers.Timer, comparatively troublesome.