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. ...
随机推荐
- 1066. Root of AVL Tree (25)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- R语言基础(一) 可视化基础
##数据获取 x1=round(runif(100,min=80,max=100)) x2=round(rnorm(100,mean=80, sd=7)) x3=round(rnorm(100,mea ...
- CentOS 6.3 配置 yum
ContOS 配置yum:1.cd /etc/yum.repos.d2.创建个任意目录,将所有文件移动到创建的目录中,除了CentOS-Media.repo3.编辑CentOS-Media.repov ...
- mysqldump 参数说明
mysqldump参数说明 --all-databases , -A 导出全部数据库. mysqldump -uroot -p --all-databases --all-tablespaces , ...
- RCP,TCP,C/S,B/S
RCP: RICH CLIENT PROGRAM 胖客户端 TCP: THIN CLIENT PROGRAM 瘦客户端 CS: CLIENT SERVER 客户端/服务 ...
- iTween基础之Value(数值过度)
一.基础介绍:二.基础属性 原文地址:http://blog.csdn.net/dingkun520wy/article/details/50550527 一.基础介绍 Value有一个函数 Valu ...
- SqlServer里DateTime转字符串
Select CONVERT(varchar(100), GETDATE(), 8):14:53:14 Select CONVERT(varchar(100), GETDATE(), 9): 06 ...
- Week1 Team Homework #2 Introduction of team member with photos
小组成员介绍 组长:黄剑锟 11061164 组员:顾泽鹏 11061160 组员:周辰光 11061154 组员:龚少波 11061167 组 ...
- C# ASP.NET系统缓存全解析
原文:http://blog.csdn.net/wyxhd2008/article/details/8076105 目录(?)[-] 系统缓存的概述 页面输出缓存 页面局部缓存 文件缓存依赖 数据库缓 ...
- BAT CMD 批处理文件脚本 -1
http://www.cnblogs.com/linglizeng/archive/2010/01/29/Bat-CMD-ChineseVerion.html 1. 综述 ...