WPF中定时器Timer与DispatcherTimer的用法
最近的工作项目中需要定时更新UI控件中的数据,这时候第一反应肯定会想到去使用System.Timers.Timer定时更新UI控件,但是程序运行后,会发现程序崩溃了。报的异常为“调用线程无法访问此对象,因为另一个线程拥有该对象。”,网上查找了原因,Timer的触发事件与UI不是属于同一个线程,所以说在Timer的触发事件去更新UI时,会造成UI对象被占用的问题。网上说,可以尝试用DispatcherTimer这个定时器去更新UI,于是我做个一个demo测试了一下,果然是可行的。
上面是一个WPF窗口,点击计时按钮,编辑框中显示计时次数
1.使用System.Timers.Timer更新编辑框中的数据
namespace Timer
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
System.Timers.Timer timer = null;
private static int nCount = ;
public MainWindow()
{
InitializeComponent();
timer = new System.Timers.Timer();
timer.AutoReset = true;
timer.Elapsed += new ElapsedEventHandler(TimeAction);
}
private void TimeAction(object sender, ElapsedEventArgs e)
{
if (!String.IsNullOrEmpty(txt_TimeCount.Text))
{
//计时次数大于10时,则关闭定时器
if (Convert.ToInt32(txt_TimeCount.Text) > )
{
timer.Stop();
}
} txt_TimeCount.Text = Convert.ToString(nCount++);
}
private void btn_Time_Click(object sender, RoutedEventArgs e)
{
timer.Start();
}
}
}
当点击按钮后,会很明显的出现上述所说的异常问题
2.使用DispatcherTimer更新编辑框中的数据
namespace Timer
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer dispatcherTimer = null;
private static int nCount = ;
public MainWindow()
{
InitializeComponent();
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = new TimeSpan(, , );
dispatcherTimer.Tick += new EventHandler(TimeAction); } private void TimeAction(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txt_TimeCount.Text))
{
//计时次数大于10时,则关闭定时器
if (Convert.ToInt32(txt_TimeCount.Text) > )
{
dispatcherTimer.Stop();
}
} txt_TimeCount.Text = Convert.ToString(nCount++);
}
private void btn_Time_Click(object sender, RoutedEventArgs e)
{
dispatcherTimer.Start();
}
}
}
输出结果如下,可以看出程序能够正常的运行
WPF中定时器Timer与DispatcherTimer的用法的更多相关文章
- WPF中定时器与进度条的配合使用
WPF中定时器使用的注意事项: WPF需要使用System.Windows.Threading.DispatcherTimer定时器,而不能使用System.Timers.Timer定时器.因为Sys ...
- Java中定时器Timer致命缺点(附学习方法)
简介 这篇文章我一直在纠结到底要不要写,不想写一来因为定时器用法比较简单,二来是面试中也不常问.后来还是决定写了主要是想把自己分析问题思路分享给大家,让大家在学习过程中能够参考,学习态度我相信大部分人 ...
- WPF 中定时器的使用
DispatcherTimer timer; private void Window_Loaded(object sender, RoutedEventArgs e) { timer = new Di ...
- 解决windows 服务中定时器timer 定时偶尔失效 问题
最近做个windows 服务,功能是:定时执行一个任务:自动登录到一个网站后,点击相关网面上的按钮button. 在处理的过程中发现定时器老是不定时的失效,失效时间没有规律. 由于刚开始处于测试阶段, ...
- 【WPF】 Timer与 dispatcherTimer 在wpf中你应该用哪个?
源:Roboby 1.timer或重复生成timer事件,dispatchertimer是集成到队列中的一个时钟.2.dispatchertimer更适合在wpf中访问UI线程上的元素 3.Dispa ...
- wpf中应该使用c#四种定时器中的DispatcherTimer
c#中有四种定时器 1:System.Threading.Timer 使用: private System.Threading.Timer timerClose; timerClose = new S ...
- WPF中Timer与DispatcherTimer类的区别
前几天在WPF中写了一个轨迹回放的功能,我想稍微做过类似项目的,都晓得采用一个时间控件或者时间对象作为调度器,我在这么做的时候,出现了问题,于是将程序中的Timer换成了DispatchTimer,然 ...
- .NET中的Timer类型用法详解
这篇文章主要介绍了.NET中的Timer类型用法,较为详细的分析了Timer类型在各种环境下的用法,需要的朋友可以参考下 在.NET FrameWork中有多个Timer,那么怎么根据实际情况进行 ...
- WPF中timer的使用
Timer控件/ System.Timers.Timer 不能用于WPF中.在WPF中,定时器为 DispatcherTimer. 使用方法如下: private DispatcherTimer ti ...
随机推荐
- 阿里八八Beta冲刺博客集合贴
Scrum 阿里八八β阶段Scrum(1/5) 阿里八八β阶段Scrum(2/5) 阿里八八β阶段Scrum(3/5) 阿里八八β阶段Scrum(4/5) 阿里八八β阶段Scrum(5/5) 总结 阿 ...
- 《Java大学教程》—第14章 抽象、继承和接口
自测题:1. 解释抽象和抽象数据类型的概念.P333抽象的概念是仅仅关注对象可以完成什么工作,而不必担心如何完成工作的细节.类模板通常被称为抽象数据类型.因为这类数据暴露给用户的所有信息仅仅是方 ...
- [docker][win10]安装的坑
右键这个小图标,先signin,注意这里是ID 不是邮箱 image.png 可能starting 时候就报错说 “Containers feature is not enabled” 或者 ...
- 用PHP打造一个高性能好用的网站
用PHP打造一个高性能好用的网站 1. 说到高可用的话要提一下redis,用过的都知道redis是一个具备数据库特征的nosql,正好弥补了PHP的瓶颈,个人认为PHP的 瓶颈在于数据库,像Apach ...
- 16.ajax_case02
# 抓取当当网书评 # http://product.dangdang.com/25340451.html import json import requests from lxml import e ...
- 只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句
错误信息:只有 assignment.call.increment.decrement 和 new 对象表达式可用作语句: 分析:发生这种情况一般是在赋值时把“=”写成了“==”,例如:textBox ...
- java读取properties中文乱码
1 确认properties文件的编码是utf-8 2 采用流的方式读取文件,设置编码为utf-8 public class ErrorCodeConfig { static Properties p ...
- PHP 2 语句 数据类型 字符串函数 常量
在 PHP 中,有两种基本的输出方法:echo 和 print. 在本教程中,我们几乎在每个例子中都会用到 echo 和 print.因此,本节为您讲解更多关于这两条输出语句的知识. PHP echo ...
- 在flask中使用swagger(flasgger使用方法及效果展示)
一. 部分代码及效果 from flask import Flask from flasgger import Swagger import config app = Flask(__name__) ...
- 转载 mvc:message-converters简单介绍 https://www.cnblogs.com/liaojie970/p/7736098.html
mvc:message-converters简单介绍 说说@ResponseBody注解,很明显这个注解就是将方法的返回值作为reponse的body部分.我们进一步分析下这个过程涉及到的内容,首先就 ...