WPF中Timer与DispatcherTimer类的区别
前几天在WPF中写了一个轨迹回放的功能,我想稍微做过类似项目的,都晓得采用一个时间控件或者时间对象作为调度器,我在这么做的时候,出现了问题,于是将程序中的Timer换成了DispatchTimer,然后就可以了,特意在网上找了下这两者的区别,看到一篇比较详细的,并且有代码的博文,我就直接引用了,原文地址:http://www.cnblogs.com/zhchbin/archive/2012/03/06/2381693.html,我只附加上原文的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.Timers; namespace TimerTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Timer aTimer = null;
public MainWindow()
{
InitializeComponent(); aTimer = new Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 5 seconds.
aTimer.Interval = ;
aTimer.Enabled = true;
aTimer.Start();
} private void OnTimedEvent(object source, ElapsedEventArgs e)
{
timeLabel.Content = DateTime.Now.ToUniversalTime();
}
}
}
using System;
using System.Windows;
using System.Timers;
using System.Windows.Threading; namespace TimerTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Timer aTimer = null; private delegate void TimerDispatcherDelegate(); public MainWindow()
{
InitializeComponent(); aTimer = new Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = ;
aTimer.Enabled = true;
} private void OnTimedEvent(object sender, EventArgs e)
{
this.Dispatcher.Invoke(DispatcherPriority.Normal,
new TimerDispatcherDelegate(updateUI));
} private void updateUI()
{
timeLabel.Content = DateTime.Now.ToUniversalTime();
}
}
}
通过这些,我想到,其实多线程访问UI的都是相通的,在Winform中为了更新UI,我们都会有带有Invoke或者委托来实现,而Timer要用来更新,主要看这个Timer是否多线程的。
WPF中Timer与DispatcherTimer类的区别的更多相关文章
- 【WPF】 Timer与 dispatcherTimer 在wpf中你应该用哪个?
源:Roboby 1.timer或重复生成timer事件,dispatchertimer是集成到队列中的一个时钟.2.dispatchertimer更适合在wpf中访问UI线程上的元素 3.Dispa ...
- WPF中timer的使用
Timer控件/ System.Timers.Timer 不能用于WPF中.在WPF中,定时器为 DispatcherTimer. 使用方法如下: private DispatcherTimer ti ...
- C#中结构体和类的区别
结构体和类同样能够定义字段,方法和构造函数,都能实例化对象,这样看来结构体和类的功能好像是一样的了,但是他们在数据的存储上是不一样的 C#结构体和类的区别问题:这两种数据类型的本质区别主要是各自指向的 ...
- C++中结构体与类的区别 2
这里有两种情况下的区别.(1)C的struct与C++的class的区别.(2)C++中的struct和class的区别.在第一种情况下,struct与class有着非常明显的区别.C是一种过程化的语 ...
- C#中结构体与类的区别
一.结构体和类非常相似 1,定义和使用非常相似,例子如下:public struct Student{ string Name; int Age;}public class Questio ...
- C++中结构体与类的区别(struct与class的区别)
转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...
- C++中结构体与类的区别 1
转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...
- WPF中使用定时器 DispatcherTimer 做TCP连接中的心跳 HeartBeat
开发过程中经常遇到定时触发的需求,如:TCP/IP连接中,使用心跳包保持连接或检测连接是否已经中断. WPF中有多种定时器: 1.using System.Windows.Threading; 代码如 ...
- C++中结构体与类的区别(结构不能被继承,默认是public,在堆栈中创建,是值类型,而类是引用类型)good
结构是一种用关键字struct声明的自定义数据类型.与类相似,也可以包含构造函数,常数,字段,方法,属性,索引器,运算符和嵌套类型等,不过,结构是值类型. 1.结构的构造函数和类的构造函数不同. a. ...
随机推荐
- 2014年互联网IT待遇(包括国内民企、外企、金融机构)
一.民企 1. 百度 13k*14.6,special 14~17k*14.6 开发类 13K*14.6 (2014) 测试类.前端类 12K*14.6 (2014) 2. 腾讯 11.5k*16,s ...
- apache配置VirtualHost(windows)
以下方式适合原生 Apache, XAMPP 和WAMP 套件. 1. 打开目录 {Apache2 安装目录}\conf\extra\, 找到 httpd-vhosts.conf 文件. 2. 仿照例 ...
- python之range(), xrange()
可以这样理解: range()立即执行,返回结果 xrange()延迟执行,需要时再返回结果.
- ngnix 下配置多域名跳转 配置方法
自己申请了多个域名,统一使用 http://goipc.cn/ 格式访问网站 server_name goipc.cn ; server_name www.goipc.cn ; server_name ...
- Unity3D实现立体迷宫寻宝
Unity3D实现立体迷宫寻宝 这个小游戏是一个白痴在一个昏暗的房间走动找到关键得分点,然后通关游戏.入门Unity3D做的第一款游戏,比较无聊,但实现了一般的游戏功能.如,人物控制,碰撞检测,主控制 ...
- 微软职位内部推荐-Senior SDE for Windows App Experience
微软近期Open的职位: Job posting title: Senior Software Development Engineer Location: China, Beijing Divisi ...
- 【经验】Angularjs 中使用 layDate 日期控件
layDate 控件地址:http://laydate.layui.com/ 前情:原来系统中使用的日期控件是UI bootstrap(地址:https://angular-ui.github.io/ ...
- android 开启或者隐藏软键盘
一. 隐藏软键盘方法一(注:此方法本人使用时发现isActivie()失效,建议还是用其他方法..): InputMethodManager imm = (InputMethodManager)get ...
- 3573: [Hnoi2014]米特运输 - BZOJ
Description米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储存一直是一个大问题. D星上有N个城市,我们将其顺序编号为1到N,1号 ...
- Chrome浏览器下调试SASS
网上说的方案各有各的说法,尝试多个才找到有效方案,为避免后来者麻烦,现在讲讲如何调试 笔者ruby版本为3.4.19,因为sass文件的编译是需要ruby环境的,使用sass前需要先安装! 第一步(核 ...