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 ...
随机推荐
- 软件测试——Peer Review
一.什么是peer review peer review是一种通过作者的同行来确认缺陷和需要变更区域的检查方法.需要进行同行评审的特定产品在定义项目软件过程的时候被确定并且作为软件开发计划的一部分被安 ...
- ElasticSearch 基础概念学习(未完)
1.基本定义 摘自百度百科 elasticseaElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elastic ...
- Linux Tomcat 7.0 管理员登录时"401 Unauthorized" 问题解决方法
http://blog.csdn.net/u010359532/article/details/36055291 tomcat 7.0没有默认值,需要在conf的tomcat-users.xml中设置 ...
- TensorMask
原文地址:https://arxiv.org/pdf/1903.12174.pdf 论文阅读:http://www.zhenzhujue.cn/article-36456-1.html https:/ ...
- 33-wxpython多个frame之间的信息共享
https://blog.csdn.net/xyisv/article/details/78576932 https://blog.csdn.net/tianmaxingkong_/article/d ...
- Ninject在.NET WebForm和MVC中的使用
1.建立项目:Models/BLL/DAL/IBLL/IDAL/WebSite 2.WebSite要引用其余几个项目 3.NuGet中搜索Ninject,安装 Ninject.Ninject.Web. ...
- springboot2.0整合jpa
在整合的遇到各种坑,以下是我整合的流程 1.pom.xml文件 <dependencies> <dependency> <groupId>org.springfra ...
- 复利计算--web版--总结--软件工程
复利计算项目 估计用时 实际用时 时间(小时) 5.5小时 6.5小时 总共代码行 500 550 功能包含 单利/复利计算,本金计算,求投资年限,求投资项目利率估计 (计算利息和,计算时间,计算 ...
- 使用delphi 开发多层应用(二十一)使用XE5 RESTClient 直接访问kbmmw 数据库
delphi XE5 出来了,增加了android 的开发支持,另外增加了一个RESTClient 来支持访问REST 服务器. 这个功能非常强大,可以直接使用非常多的REST 服务器.同时也可以支持 ...
- System.Web.HttpUtility VS System.Web.HttpServerUtility VS System.Net.WebUtility
HttpUtility 类作为 HttpServerUtility 类的内部使用,HttpServerUtility 通过System.Web.UI.Page.Server属性(WebForm)/Co ...