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个重载的构造 ...
随机推荐
- Python+js进行逆向编程加密MD5格式
一.安装nodejs 二.安装:pip install PyExecJs 三.js源文件Md5格式存放本地,如下 var n = {}function l(t, e) {var n = (65535 ...
- PAT乙级:1014 福尔摩斯的约会 (20分)
PAT乙级:1014 福尔摩斯的约会 (20分) 题干 大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk ...
- Leetcode 春季打卡活动 第一题:225. 用队列实现栈
Leetcode 春季打卡活动 第一题:225. 用队列实现栈 Leetcode 春季打卡活动 第一题:225. 用队列实现栈 解题思路 这里用了非常简单的思路,就是在push函数上做点操作,让队头总 ...
- C语言学习之基本数据类型【一】
近期学习鸿蒙硬件物联网开发,用到的开发语言是C: 一.基础语法:第一个案例: 命令 gcc hello.c #include <stdio.h> //stdio.h 是一个头文件 , #i ...
- lwIP(Light Weight IP)协议
信号量 信号量结构体:struct sys_semt struct _sys_sem { void *sem; }; err_t sys_sem_new(sys_sem_t *sem, u8_t c ...
- KMP子串匹配(只能匹配出唯一子串)
using namespace std; #include <iostream> #include<string> //自定义字符串存储结构String(包括char数组.le ...
- 构建后端第1篇之---springcloud项目依赖分析
张艳涛写于2021-2-2日 springcloud是springboot工程+cloud依赖,从这个角度来分析,使用springcloud实际上就是添加springcloud的某个以来比如eurek ...
- intouch制作历史报警查询(时间查询,筛选关键字)
在项目中,intouch制作历史报警查询已属于标配功能,如何做出按时间以及关键字来进行综合查询,提高历史报警查询效率仍然是一个值得研究的问题,接下来参考网上文章自己总结下如何制作. 1.DTPicke ...
- 线程状态Thread.State
线程状态Thread.State 线程状态.线程可以处于下列状态之一: NEW 至今尚未启动的线程处于这种状态. RUNNABLE 正在 Java 虚拟机中执行的线程处于这种状态. BLOCKED 受 ...
- Linux上搭建zookeeper服务注册中心
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...