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个重载的构造 ...
随机推荐
- 实验 2 Scala 编程初级实践
实验 2 Scala 编程初级实践 一.实验目的 1.掌握 Scala 语言的基本语法.数据结构和控制结构: 2.掌握面向对象编程的基础知识,能够编写自定义类和特质: 3.掌握函数式编程的基础知识,能 ...
- Spring总结之AOP
一.Spring AOP简介(百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring 框架中的一个重 ...
- Spring 学习笔记(2) Spring Bean
一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...
- 每天五分钟Go - 闭包
闭包的示例代码 func getSequence() func() int{ i:=0 return func() int { i+=1 return i } } 首先,函数名getSequence, ...
- HttpClient(七)
一.定义 1.什么是HttpClient?在什么场景要用到HttpClient? http协议可以说是现在Internet上面最重要,使用最多的协议之一了,越来越多的java应用需要使用http协议来 ...
- springboot+mybatis+mysql 利用mybatis自动生成sql语句
工具和环境 idea,mysql,JDK1.8 效果图如下 结构图如下 java resources sql文件 /* Navicat MySQL Data Transfer Source Serve ...
- python里的StringIO
Python2中StringIO调用方法如下: import StringIO iost = StringIO.StringIO() Python3中已将StringIO归入io,调用方法如下: im ...
- python -- 负数平方根-虚数的使用
负数的平方根是虚数. 不能使用sqrt,因为它只能处理浮点数,而虚数是完全不同的--这也是由另外一个叫做cmath(即 complex math, 复数)的模块来实现这些功能的原因. >> ...
- PHP:字符串转数组,数组转字符串;字符串截取、替换、查找
字符串转数组$str = 'one|two|three|four'; print_r(explode('|', $str)); //explode 以字符串分割字符串到数组 $str = 'one t ...
- 自行搭建网站和APP统计平台
做过网站运营分析的朋友,一定知道 Google 统计.友盟统计以及百度统计,它们都是非常优秀的统计平台. 但不管怎么样,数据并没有掌握在网站拥有者的手中.有时候,某些业务场景不适合使用第三方统计平台, ...