代码:

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界面的更多相关文章

  1. [转]Android定时刷新UI界面----Handler

    本文转自:http://blog.csdn.net/macong01/article/details/7479266 本想做一个软件可以对UI界面进行定时更新,找了一些资料,先贴一个简单的定时更新界面 ...

  2. Android 子线程无法刷新UI界面

    问题:在Android开发中,子线程无法直接更改UI界面视图的刷新 这个时候 Handler 起到了至关重要的作用. 简单来说 , Handler就是用来传递消息的. Handler可以当成子线程与主 ...

  3. Flutter获取远程数据 刷新UI界面

    import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; void main() => r ...

  4. WPF之设计UI界面

    一些后面再细看的内容: Document Outline窗口有什么帮助? 1.Panel控件 为了便于组织窗口内很多的UI元素,要使用Panel控件(也称为布局管理器).Panel容纳所有的窗口UI元 ...

  5. 1、简单的BackGroundWorker多线程时时刷新UI界面,并显示进度

    BackGroundWorker是微软提供的封装好了的,非常实用的控件,我们可以在控件中将其拖到Winform之中,然后简单的系统生成代码式的编辑事件处理. 以下是,比较经典且简单的实用,后面的一篇较 ...

  6. winform刷新UI界面

    this.Invoke(new Action(() => { // 更新使用次数 this.labCount.Text = count; }));

  7. 线程操作UI界面的方法

    以前一般都是用BeginInvoke来刷新UI界面,现在采用 SynchronizationContext 来刷新,写起来清楚多了. SynchronizationContext synchroniz ...

  8. 一种WPF在后台线程更新UI界面的简便方法

    WPF框架规定只有UI线程(主线程)可以更新界面,所有其他后台线程无法直接更新界面.幸好,WPF提供的SynchronizationContext类以及C#的Lambda表达式提供了一种方便的解决方法 ...

  9. 【WPF】修改数据层ViewModel后,UI界面未同步更新

    界面:WPF(MVVM)中将集合类控件ItemsControl的ItemsSource绑定到了ViewModel中的ObservableCollection列表,ItemsControl.ItemTe ...

随机推荐

  1. delete,truncate,drop的区别

    操作 删除对象 表和索引的空间 是否回滚 时间 delete table和view的数据,可以使用where精确删除,删除会触发触发器 表或索引的空间不变化 是dml,可以rollback回滚 最慢 ...

  2. 剖析tcp与udp及应用场景协议方案选择

    什么是TCP和UDP TCP和UDP是TCP/IP协议中的两个传输层协议,它们使用IP路由功能把数据包发送到目的地,从而为应用程序及应用层协议(包 括:HTTP.SMTP.SNMP.FTP和Telne ...

  3. (转)Chrome开发者工具不完全指南(一、基础功能篇)

    本篇转载自卖烧烤夫斯基,并做了小部分的修改. 原文地址:Chrome开发者工具不完全指南(一.基础功能篇) 原作者:卖烧烤夫斯基 就算你不是一名前端开发工程师,相信你也不会对Chrome浏览器感到陌生 ...

  4. 让别人能登陆你的mysql

    线上的数据库肯定是不能轻易在开发新功能的时候动的,如果你的数据库跟线上不一样了又没有新数据库的备份,就很麻烦. 当然去动线上数据库,出了什么问题我是不想背锅的. 最稳健的办法!让管理线上数据库的同学, ...

  5. np归纳总结(全)第一天

    1.概述 1.np.array()  # 将列表转换为数组 import numpy as np array = [1, 2, 3, 4, 5] array = np.array(array) 2.. ...

  6. STL string 常用函数(转)

    string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...

  7. 有单例模式 Singleton 涉及的一些防止类被继承的东西

    c#中 : ------------------------------- 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承. java中: ------------------ ...

  8. 快速变幻AABB的顶点

    [快速变幻AABB的顶点] 当要变幻一个AABB时,可以快速计算变幻后顶点的AABB.当有旋转时,根据8个顶点变幻后的AABB可能会更大. AABB的八个顶点需分别作如下变幻: 注意到为了使 x' 最 ...

  9. Python_07-常用函数

    1     python常用常用函数 1.1      常用内置函数 1.2      类型转换函数 1.3      和操作系统相关的调用 1.3.1       操作举例 1.4      用os ...

  10. java文档打包成压缩包并且下载

    需求,根据产品ID查询产品详情,产品详情会返回产品的一些文案,以及图片的url.需要做成,将文案信息记录在一个txt文档中,然后图片下载到文件夹,最后下载到本地,下载后自动删除刚才生成的文件夹以及文件 ...