首先,我觉得三种计时器最大的区别是: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)的更多相关文章

  1. Forms.Timer、Timers.Timer、Threading.Timer的研究

    .NET Framework里面提供了三种Timer System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer 一.S ...

  2. System.Timers.Timer(定时器)

    1.System.Timers命名空间下的Timer类.System.Timers.Timer类:定义一个System.Timers.Timer对象,然后绑定Elapsed事件,通过Start()方法 ...

  3. 問題排查:沒有任何多載符合 System.Timers.ElapsedEventHandler 委派

    這是在實作當前專案最後一個關鍵功能:提醒通知 所遇到的奇怪狀況 目前的設想,是以 Windows Form 結合 Timer,當作發送通知的載體 大家都知道在 C# 的環境裡,有三種內建的 Timer ...

  4. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

  5. 【C#/WPF】用System.Timers.Timer计时器做浮窗广告

    需求:鼠标静止一段时间后,显示浮窗广告. 思路:界面XAML写好一个专门显示浮窗广告的Canvas,先设为不可见Visibility=”Collapsed”,然后用System.Timers.Time ...

  6. C# 定时执行方法: System.Timers.Timer用法示例

    System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒        private void Form1_Load(ob ...

  7. 简述System.Windows.Forms.Timer 与System.Timers.Timer用法区别

    System.Windows.Forms.Timer 基于窗体应用程序 阻塞同步 单线程 timer中处理时间较长则导致定时误差极大. System.Timers.Timer 基于服务 非阻塞异步 多 ...

  8. System.Timers.Timer用法

    System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(object se ...

  9. System.Threading.Timer 定时器的用法

    System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此  .Net Framework 提供了5个重载的构造 ...

随机推荐

  1. Qt Model/view 小实例 文件目录浏览器

    1. 文件目录浏览器 直接在main.cpp文件中添加下列代码 #include "mainwindow.h" #include <QApplication> #inc ...

  2. 求数组的子数组之和的最大值II

    这次在求数组的子数组之和的最大值的条件下又增加了新的约束:  1.要求数组从文件读取.      2.如果输入的数组很大,  并且有很多大的数字,  就会产生比较大的结果 (考虑一下数的溢出), 请保 ...

  3. JavaScript学习笔记:你必须要懂的原生JS(一)

    1.原始类型有哪几种?null是对象吗?原始数据类型和复杂数据类型存储有什么区别? 原始类型有6种,分别是undefined,null,bool,string,number,symbol(ES6新增) ...

  4. 7Java基础补充

    1.标准Java bean写法 包括:private修饰的成员变量.getter和setter以及无参和有多个参数的有参构造方法 2.String原理 String底层是字节数组byte[]. Str ...

  5. odoo14学习----x2many操作与图片设置继承image.mixin

    三种方式实现数据更新 一,如上所述 二,通过对数据集调用update({'key':value,'key1':value1..})更新数据集 三,调用write函数,与update类似,传递字典.   ...

  6. Python -- 值转换为字符串的两种机制

    可以通过以下两个函数来使用这两种机制:一是通过str函数,它会把值转换为合理形式的字符串,以便用户可以理解:而repr会创建一个字符串,它以合法的Python表达式的形式来表示值.下面是一些例子: & ...

  7. Python将字符串转化为对应类名的两种方法

    way first: 1 from django.utils.module_loading import import_string 2 ValidationError = import_string ...

  8. 深入刨析tomcat 之---第13篇 tomcat的三种部署方法

    writedby 张艳涛 一般我们都知道将web 应用打成war包,放到tomcat的webapp目录下,就是部署了,这是部署方法1 第2种部署方法我们也知道,就是讲web应用的文件夹拷贝到webap ...

  9. memcache(11211)未授权访问

    1.安装Memcache服务端 sudo apt-get install memcached 2.启动服务 sudo memcached -d -m 128 -p 11211 -u root 3.ap ...

  10. 根据随身固态U盘卷标搜索U盘盘符并打开文件的批处理脚本.bat 徐晓亮 595076941@qq.com 2019年12月19日6点50分

    @Echo offRem 根据随身固态U盘卷标搜索U盘盘符并打开文件的批处理脚本.batRem 徐晓亮 595076941@qq.com 2019年12月19日6点50分 Rem 此批处理脚本源代码的 ...