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 ...
随机推荐
- 好记性不如烂笔头-linux学习笔记4apache相关知识
apache 启动有2种模式 1是prefork模式,每个进程对应一个线程,如果是比较稳定的平台,那么prefork模式是worker模式 比较好,效率高,但是吃的内存比较大. 2 如果是高负载高并发 ...
- CommonDialog控件
'要先单击“工程-部件”,显示“部件”对话框,将“Microsoft Common Dialog control 6.0(SP6)”选中,在工具栏就多出了一个CommonDialog控件图标,将其添加 ...
- aria2自动更新BT Tracker服务器列表脚本
vi /root/trackers-list-aria2.sh 内容如下: #!/bin/bash #/usr/sbin/service aria2 stop list=`wget -qO- http ...
- eval函数的使用之一
把从后台传到前端的json格式的字符串转成json success: function(data){ var json =eval("("+data+")"); ...
- 理解ServletRequest和ServletResponse
博客地址:http://blog.51cto.com/lavasoft/275586
- Unity5 Shader Stripping 导致 LightMap 全部丢失的解决方法
当使用 SceneManager.LoadScene 的时候,会自动载入LightMap 和 NavMesh的数据.然后再对MeshRender 进行指定 LightMapIndex 以及 Light ...
- ASP.NET 网页动态添加客户端脚本
在System.Web.UI.Page类中包含了RegisterStarupScript()和RegisterClientScriptBlock()两个方法,使用这两个方法可以实现向Web页面动态添加 ...
- ubuntu 基础环境
一.序言 这里记录了安装ubuntu 系统,以及里面常用的东西,jdk,idea,maven,svn,git 等等工具的安装,因为这些动作不是经常操作的,因此这里做一个记录,方便新手或者忘记的时候看看 ...
- ubuntu登陆无限循环
现象:在Ubuntu登陆界面输入密码之后,黑屏一闪并且出现了check battery state之类的文字之后,又跳转到登录界面. 原因:主目录下的.Xauthority文件拥有者变成了root,从 ...
- JavaScript 语法总结2
1. 对象的toString()和valueOf(). - toString() 和Java中的toString() 一样 - valueOf(), 和toString() 都是用来进行类型转换的方法 ...