WPF定时刷新UI界面
代码:
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 timerNotice = null; public MainPage()
{
InitializeComponent();
} private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
#region 通知公告
if (timerNotice == null)
{
BindNotice(); timerNotice = new System.Timers.Timer();
timerNotice.Elapsed += new System.Timers.ElapsedEventHandler((o, eea) =>
{
BindNotice();
});
timerNotice.Interval = * ;
timerNotice.Start();
}
#endregion
} private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
{ } #region 绑定通知公告
private void BindNotice()
{
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
try
{
int total = ;
TES_NOTICE info = new TES_NOTICE();
IList<TES_NOTICE> list = new List<TES_NOTICE>(); list = HI.Get<INoticeService>().GetListPage(null, DateTime.MinValue, DateTime.MinValue, , , ref total); Dispatcher.Invoke(new Action(() =>
{
noticeListView.ItemsSource = list;
}));
}
catch
{ }
});
}
#endregion }
}
说明:在 System.Timers.Timer 的事件中使用 BackgroundWorker 是无效的,即如下代码不能正常刷新界面:
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 timerNotice = null; public MainPage()
{
InitializeComponent();
} private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
#region 通知公告
if (timerNotice == null)
{
BindNotice(); timerNotice = new System.Timers.Timer();
timerNotice.Elapsed += new System.Timers.ElapsedEventHandler((o, eea) =>
{
BindNotice();
});
timerNotice.Interval = * ;
timerNotice.Start();
}
#endregion
} private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
{ } #region 绑定通知公告
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 }
}
也可以使用 DispatcherTimer 刷新界面,但耗时的操作不能放在DispatcherTimer的事件中执行,否则界面会卡,那么耗时的定时操作,比如查询数据库,需要再用一个 System.Timers.Timer,相对比较麻烦。
WPF定时刷新UI界面的更多相关文章
- [转]Android定时刷新UI界面----Handler
本文转自:http://blog.csdn.net/macong01/article/details/7479266 本想做一个软件可以对UI界面进行定时更新,找了一些资料,先贴一个简单的定时更新界面 ...
- Android 子线程无法刷新UI界面
问题:在Android开发中,子线程无法直接更改UI界面视图的刷新 这个时候 Handler 起到了至关重要的作用. 简单来说 , Handler就是用来传递消息的. Handler可以当成子线程与主 ...
- Flutter获取远程数据 刷新UI界面
import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; void main() => r ...
- WPF之设计UI界面
一些后面再细看的内容: Document Outline窗口有什么帮助? 1.Panel控件 为了便于组织窗口内很多的UI元素,要使用Panel控件(也称为布局管理器).Panel容纳所有的窗口UI元 ...
- 1、简单的BackGroundWorker多线程时时刷新UI界面,并显示进度
BackGroundWorker是微软提供的封装好了的,非常实用的控件,我们可以在控件中将其拖到Winform之中,然后简单的系统生成代码式的编辑事件处理. 以下是,比较经典且简单的实用,后面的一篇较 ...
- winform刷新UI界面
this.Invoke(new Action(() => { // 更新使用次数 this.labCount.Text = count; }));
- 线程操作UI界面的方法
以前一般都是用BeginInvoke来刷新UI界面,现在采用 SynchronizationContext 来刷新,写起来清楚多了. SynchronizationContext synchroniz ...
- 一种WPF在后台线程更新UI界面的简便方法
WPF框架规定只有UI线程(主线程)可以更新界面,所有其他后台线程无法直接更新界面.幸好,WPF提供的SynchronizationContext类以及C#的Lambda表达式提供了一种方便的解决方法 ...
- 【WPF】修改数据层ViewModel后,UI界面未同步更新
界面:WPF(MVVM)中将集合类控件ItemsControl的ItemsSource绑定到了ViewModel中的ObservableCollection列表,ItemsControl.ItemTe ...
随机推荐
- hive 创建/删除/截断 表(翻译自Hive wiki)
这里罗列常用操作,更多参考 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL- ...
- 为什么重写equals后要重写hashCode
equals和hashCode的关系 要搞清楚题目中的问题就必须搞明白equals方法和hashCode方法分别是什么,和诞生的原因,当搞明白了这一点其实题目就不算是个问题了,下面我们来探讨分别探讨一 ...
- 关于phpmailer邮件发送
今天有个需求,要把phpmailer集成到框架里面 所以我去官方下载了 phpmail5.2.6 地址在 https://github.com/PHPMailer/PHPMailer/releases ...
- Lua加密
两种方式:一种用luac,一种用luajit luac加密: 1.lua本身可以使用luac将脚本编译为字节码(bytecode)从而实现加密,去官网下载Lua源代码包(http://www.lua. ...
- java线程池相关接口Executor和ExecutorService
在线程池的api中,Executor接口是最上层的接口,内部只有一个方法.如下: public interface Executor { void execute(Runnable command); ...
- 阿里云自定义监控tomcat进程数
阿里云提供自定义监控SDK,这有助于我们定制化的根据自身业务来做监控,下面我就根据业务需求来介绍一个简单的自定义监控配置. 阿里提供了2个版本的自定义监控接口:自定义监控SDK(python版) :c ...
- Cook-Torrence Illumination Model 的一些数学说明
Cook-Torrence 光照模型如下: 这个Io就是计算后最终的光强,主要是用来计算镜面反射光,漫反射和环境光的计算和Phong模型一致. F:Fresnel反射系数.主要用来说明反射光强度占入射 ...
- 多因子降维法(MDR,Multifactor Dimensionality Reduction )
多因子降维法(MDR,multifactor dimensionality reduction) 多因子降维法(MDR,Multifactor Dimensionality Reduction ) ...
- SetupDiEnumDeviceInfo
BOOLEANSetupDiEnumDeviceInfo(IN HDEVINFO DeviceInfoSet,IN DWORD MemberIndex,OUT PSP_DEVINFO_DATA Dev ...
- [OS] 修改屏幕分辨率(用Remote Desktop Connection 或者 用工具:Remote Desktop Connection Manager)
用Remote Desktop Connection Remote Desktop Connection Manager