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个重载的构造 ...
随机推荐
- C语言:文件
文件是数据源的一种,最主要的作用是保存数据.在操作系统中,为了统一对各种硬件的操作,简化接口,不同的硬件设备也都被看成一个文件.对这些文件的操作,等同于对磁盘上普通文件的操作.例如: 通常把显示器称为 ...
- 高校表白App-团队冲刺第三天
今天要做什么 今天开站立会议的时候,忽然发觉在一个完整的App上好像是有一些引导页的,比如说在第一次使用App的时候,或者是在使用App的时候会出现新手指引操作. 做了什么 通过查阅资料来学习View ...
- IDEA工具-自动导包去除星号(import xx.xx.*)
打开设置>Editor>Code Style>Java>Scheme Default>Imports 设置导入类数值阈值,默认同包类是超过5个变成*,静态导入超过3个变成 ...
- MapReduce学习总结之简介
执行步骤:1)准备Map处理的输入数据 2)Mapper处理 3)Shuffle 4)Reduce处理 5)结果输出 三.mapreduce核心概念: 1)split:交由MapReduce作业来处理 ...
- ajax请求对返回数据data的处理
1,ajax请求会根据响应头的返回数据类型对返回的数据data变量进行不同的处理 $.get("data/user-permission-submit-" + ddo.manipu ...
- 在deeping上安装mariadb
1,安装的官网参考:有安装的命令和指导https://downloads.mariadb.org/mariadb/repositories/#distro=Debian&distro_rele ...
- session及cookie详解(七)
前言 文章说明 在每整理一个技术点的时候,都要清楚,为什么去记录它.是为了工作上项目的需要?还是为了搭建技术基石,为学习更高深的技术做铺垫? 让每一篇文章都不是泛泛而谈,复制粘贴,都有它对自己技术提升 ...
- 在Java web项目中防止用户注销后使用浏览器中的“后退”按钮返回注销前页面
一背景 公司安全整改, 要求:系统中对于关键业务操作应确保使用浏览器"后退"功能无法回到上一步操作界面. 提供:凭证提供所有被检查系统关键业务操作后回退视频,视频显示关键业务操作后 ...
- C++第四十一篇 -- 安装成功的第一个驱动文件
参考链接:https://blog.csdn.net/LEON1741/article/details/87291839 一.新建工程 二.写一个Driver.c #include <ntddk ...
- linux 之awk--格式化文本信息
https://www.cnblogs.com/xudong-bupt/p/3721210.html awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常 ...