前几天在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类的区别的更多相关文章

  1. 【WPF】 Timer与 dispatcherTimer 在wpf中你应该用哪个?

    源:Roboby 1.timer或重复生成timer事件,dispatchertimer是集成到队列中的一个时钟.2.dispatchertimer更适合在wpf中访问UI线程上的元素 3.Dispa ...

  2. WPF中timer的使用

    Timer控件/ System.Timers.Timer 不能用于WPF中.在WPF中,定时器为 DispatcherTimer. 使用方法如下: private DispatcherTimer ti ...

  3. C#中结构体和类的区别

    结构体和类同样能够定义字段,方法和构造函数,都能实例化对象,这样看来结构体和类的功能好像是一样的了,但是他们在数据的存储上是不一样的 C#结构体和类的区别问题:这两种数据类型的本质区别主要是各自指向的 ...

  4. C++中结构体与类的区别 2

    这里有两种情况下的区别.(1)C的struct与C++的class的区别.(2)C++中的struct和class的区别.在第一种情况下,struct与class有着非常明显的区别.C是一种过程化的语 ...

  5. C#中结构体与类的区别

    一.结构体和类非常相似 1,定义和使用非常相似,例子如下:public struct Student{    string Name;    int Age;}public class Questio ...

  6. C++中结构体与类的区别(struct与class的区别)

    转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...

  7. C++中结构体与类的区别 1

    转载来源:http://blog.sina.com.cn/s/blog_48f587a80100k630.html C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据 ...

  8. WPF中使用定时器 DispatcherTimer 做TCP连接中的心跳 HeartBeat

    开发过程中经常遇到定时触发的需求,如:TCP/IP连接中,使用心跳包保持连接或检测连接是否已经中断. WPF中有多种定时器: 1.using System.Windows.Threading; 代码如 ...

  9. C++中结构体与类的区别(结构不能被继承,默认是public,在堆栈中创建,是值类型,而类是引用类型)good

    结构是一种用关键字struct声明的自定义数据类型.与类相似,也可以包含构造函数,常数,字段,方法,属性,索引器,运算符和嵌套类型等,不过,结构是值类型. 1.结构的构造函数和类的构造函数不同. a. ...

随机推荐

  1. mysql简单操作一

    MySQL的一些简单管理: 启动MySQL服务: sudo start mysql 停止MySQL服务: sudo stop mysql 修改 MySQL 的管理员密码: sudo mysqladmi ...

  2. UML中的六大关系

    转自:http://www.cnblogs.com/shengtianlong/archive/2010/10/23/1858953.html UML定义的关系主要有六种:依赖.类属.关联.实现.聚合 ...

  3. UpdateData(false) and UpdateData(true)

    数据更新函数: UpdateData(false); 控件的关联变量的值传给控件并改变控件状态(程序--->EXE) UpdateData(true); 控件的状态传给其关联的变量(EXE--- ...

  4. nginx demo

    server_names_hash_bucket_size 512;upstream node_app { server 127.0.0.1:3000; } server { listen 80; s ...

  5. Oracle 存储过程实例2

    --创建存储过程 CREATE OR REPLACE PROCEDURE xxxxxxxxxxx_p ( --参数IN表示输入参数,OUT表示输入参数,类型可以使用任意Oracle中的合法类型. is ...

  6. ios的UIImage的两种不同的图片加载方式 tom猫

    在ios的UI交互设计时,对图片的处理是难免的:不同的处理方式会对内存有不同的影响: ********************************************************* ...

  7. 视频FMS服务器带宽成本分析

    一.现状 调查了一下,主要有两种主流方式,WebRTC或者Flash. 1. WebRTC(不支持IE浏览器,已排除):网页实时通信(英语:Web Real-Time Communication)的缩 ...

  8. struts2传map到前台出现的问题

    后台打印出的错: 2016-08-16 13:42:52.652 WARN  org.apache.struts2.json.JSONWriter     - JavaScript doesn't s ...

  9. python 链接hive

    http://blog.csdn.net/xubcing/article/details/8350287 http://www.centoscn.com/python/2014/0921/3801.h ...

  10. Codeforces Round #343 (Div. 2) C. Famil Door and Brackets

    题目链接: http://codeforces.com/contest/629/problem/C 题意: 长度为n的括号,已经知道的部分的长度为m,现在其前面和后面补充‘(',或')',使得其长度为 ...