C# 计时器用法(DispatcherTimer、System.Timers.Timer、System.Threading.Timer)
首先,我觉得三种计时器最大的区别是:DispatcherTimer触发的内容会直接转到主线程去执行(耗时操作会卡住主线程),另外两个则是在副线程执行,如果需要修改界面,则需要手动转到主线程。
DispatcherTimer:
DispatcherTimer _timer;
public void TestDispatcherTimer()
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += _timer_Tick;
_timer.Start();
}
private void _timer_Tick(object sender, EventArgs e)
{
try
{
Trace.WriteLine("Test DispatcherTimer");
_timer.Stop();
}
catch (Exception ex)
{
Trace.WriteLine("Error");
_timer.Stop();
_timer.Start();
}
}
System.Timers.Timer:
System.Timers.Timer _timer;
public void TestTimersTimer()
{
_timer = new System.Timers.Timer();
_timer.Interval = 1000; //单位毫秒
_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
} private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
Trace.WriteLine("Test System.Timers.Timer");
#region 此处展示的是把副线程的内容转到主线程
System.Windows.Application.Current.Dispatcher.Invoke(
new Action(() =>
{
Trace.WriteLine("同步切换");
}));
System.Windows.Application.Current.Dispatcher.BeginInvoke(
new Action(() =>
{
Trace.WriteLine("异步切换");
}));
#endregion
_timer.Stop();
}
catch (Exception ex)
{
Trace.WriteLine("Error");
_timer.Stop();
_timer.Start();
}
}
System.Threading.Timer:
System.Threading.Timer _timer;
private void TeseThreadingTimer()
{
_timer = new System.Threading.Timer(new System.Threading.TimerCallback(Out), null, 0, 1000); //各参数意思详见:https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.timer?redirectedfrom=MSDN&view=netframework-4.8
}
private void Out(object sender)
{
try
{
Trace.WriteLine("Test System.Threading.Timer");
}
catch (Exception ex)
{
Trace.WriteLine("Error");
}
}
private void Stop()
{
_timer.Dispose();
}
此处个人无关记载:Environment.TickCount
C# 计时器用法(DispatcherTimer、System.Timers.Timer、System.Threading.Timer)的更多相关文章
- Forms.Timer、Timers.Timer、Threading.Timer的研究
.NET Framework里面提供了三种Timer System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer 一.S ...
- System.Timers.Timer(定时器)
1.System.Timers命名空间下的Timer类.System.Timers.Timer类:定义一个System.Timers.Timer对象,然后绑定Elapsed事件,通过Start()方法 ...
- 問題排查:沒有任何多載符合 System.Timers.ElapsedEventHandler 委派
這是在實作當前專案最後一個關鍵功能:提醒通知 所遇到的奇怪狀況 目前的設想,是以 Windows Form 結合 Timer,當作發送通知的載體 大家都知道在 C# 的環境裡,有三種內建的 Timer ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- 【C#/WPF】用System.Timers.Timer计时器做浮窗广告
需求:鼠标静止一段时间后,显示浮窗广告. 思路:界面XAML写好一个专门显示浮窗广告的Canvas,先设为不可见Visibility=”Collapsed”,然后用System.Timers.Time ...
- C# 定时执行方法: System.Timers.Timer用法示例
System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(ob ...
- 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别
System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...
- System.Timers.Timer用法
System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(object se ...
- System.Threading.Timer 定时器的用法
System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此 .Net Framework 提供了5个重载的构造 ...
随机推荐
- 团队开发day06
实现功能,通过连接,获取到品牌商存入数据库的数据, 将数据返回到list集合,展示在安卓前端 出现问题:servlet响应请求返回数据格式出错,利用gson类将java类 转化为jsonobject对 ...
- 使用pymysql循环删除重复数据,并修改自增字段偏移值
创建表: CREATE TABLE `info` ( `id` tinyint NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, PRIMAR ...
- 【算法学习笔记】概率与期望DP
本文学习自 Sengxian 学长的博客 之前也在CF上写了一些概率DP的题并做过总结 建议阅读完本文再去接着阅读这篇文章:Here 前言 单纯只用到概率的题并不是很多,从现有的 OI/ACM 比赛中 ...
- Linux 安装exclipse
1,在官网下载:https://www.eclipse.org/downloads/packages/ 2,前提:安装好jdk 3,使用命令解压:tar -zxvf eclipse_xxxxxx.ta ...
- Cannot read property 'data' of undefined —— 小程序开发
由于疫情原因目前处于半下岗状态,在家的时候就研究起了小程序开发.由于是新手,所以总会遇到各种问题,顺便记录一下. wx.showModal({ title: '提示', content: '这是一个模 ...
- npm 安装、卸载模块
npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录:[npm install -g xxx]利用npm安装全局模块xxx:本地安装时将模块写入packa ...
- Ubuntu 20.04 安装kodi播放器
打开终端,执行命令在线安装 sudo apt-get install software-properties-common sudo add-apt-repository ppa:team-xbmc/ ...
- Python基础之读写xml总结
参考文章:https://blog.csdn.net/weixin_42749767/article/details/82770563 先介绍xml.dom.minidom包,有一个读写的例子 rea ...
- 第三十一篇 -- 理一下.h和.cpp的关系
今天突然想到一个问题,我们平时写代码会将代码进行分类,写到不同的cpp里,然后要用到那个类里面的函数,就直接include .h文件就好了.然后今天就在想,.h里面都是一些声明,它是怎么链接到.cpp ...
- [WUSTCTF2020]朴实无华
[WUSTCTF2020]朴实无华 考点:1.MD5碰撞 2.命令执行绕过 打开题发现编码有些问题,修改编码为utf-8 看了源码和请求包没发现什么东西,看到title想到了robots.txt文件, ...