摘要

之前学习过c#中定时器Timer的基本用法,在使用过程中,有一个问题,一直困扰着自己,就是在初始化定时器的时候,如果设置的interval过小,或者每次执行的业务非常耗时的时候,这时候该怎么处理?第一次还没执行结束,下一次已经触发了。

基础

之前学习时的一个例子:http://www.cnblogs.com/wolf-sun/p/5849229.html

一个例子

如果设置的interval比较大,而业务执行过程耗时很小,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;namespace TimerTest
{class Program{static Timer timer = new Timer();static void Main(string[] args){timer.Interval = 1000;timer.AutoReset = true;timer.Enabled = true;timer.Elapsed += timer_Elapsed;Console.Read();}static int count = 1;static void timer_Elapsed(object sender, ElapsedEventArgs e){Console.WriteLine("第{0}次触发", count.ToString());if (count == 10){timer.Enabled = false;}Console.WriteLine("当前线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId);System.Threading.Thread.Sleep(10);Console.WriteLine("第{0}次处理完成", count.ToString());count++;}}
}

执行过程

但实际中由于业务非常复杂,执行很耗时

System.Threading.Thread.Sleep(2000);

可以看到这是,已经开始乱了,线程id已经变了,如果在里面涉及到引用的类型,必然引起多个线程修改同一个变量的问题,造成并不是我们想要的结果。

当然,这个时候有很多处理方法,加锁,或者设置标致量,等本次运行结束时,再运行下一次的。但这种方式,会造成timer的空转。

加锁

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;namespace TimerTest
{class Program{static readonly object obj = new object();static Timer timer = new Timer();static void Main(string[] args){timer.Interval = 1000;timer.AutoReset = true;timer.Enabled = true;timer.Elapsed += timer_Elapsed;Console.Read();}static int count = 1;static void timer_Elapsed(object sender, ElapsedEventArgs e){lock (obj){Console.WriteLine("第{0}次触发", count.ToString());if (count == 10){timer.Enabled = false;}Console.WriteLine("当前线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId);System.Threading.Thread.Sleep(2000);Console.WriteLine("第{0}次处理完成", count.ToString());count++;}}}
}

执行

标志量

        static void timer_Elapsed(object sender, ElapsedEventArgs e){if (isRunning){isRunning = false;Console.WriteLine("第{0}次触发", count.ToString());if (count == 10){timer.Enabled = false;}Console.WriteLine("当前线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId);System.Threading.Thread.Sleep(2000);Console.WriteLine("第{0}次处理完成", count.ToString());count++;isRunning = true;}}

运行结果

但仍有另外一种方式,可以在当前处理业务的时候,将当前的timer先停止,执行完毕之后开启。

        static void timer_Elapsed(object sender, ElapsedEventArgs e){timer.Enabled = false;          if (count == 10){timer.Enabled = false;return;}Console.WriteLine("第{0}次触发", count.ToString());Console.WriteLine("当前线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId);System.Threading.Thread.Sleep(2000);Console.WriteLine("第{0}次处理完成", count.ToString());timer.Enabled = true;count++;}

执行结果

总结

可以尝试测试开启100个定时器甚至更多的进行测试比较,推荐使用处理业务之前关闭,处理结束之后开启的方式。

转载于:https://www.cnblogs.com/wolf-sun/p/6635180.html

[C#]System.Timers.Timer(2)相关推荐

  1. System.Timers.Timer的Enable、Start、Stop记录

    Timer的初始化,此时不执行theout3方法 System.Timers.Timer time = new System.Timers.Timer(); time.Interval = 1000; ...

  2. C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题

    C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题 参考文章: (1)C# System.Timers.Timer中的坑,程序异常退出后timer依然运行问题 ( ...

  3. System.Timers.Timer与System.Threading.Timer

    我最近一直在查看一些可能的计时器,而Threading.Timer和Timers.Timer对我来说是必要的(因为它们支持线程池). 我正在制作游戏,我计划使用不同类型的活动,间隔不同等. 哪个最好? ...

  4. System.Timers.Timer 多线程问题[转]

    已解决问题] System.Timers.Timer 多线程 ,同时首次进 声明: System.Timers.Timer timer = new System.Timers.Timer(); tim ...

  5. System.Windows.Forms.Timer与System.Timers.Timer的区别

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

  6. System.Timers.Timer 嵌套 System.Windows.Forms.Timer的问题

    如题"System.Timers.Timer 嵌套 System.Windows.Forms.Timer的问题",最近在项目中在类uc_Map中启用了System.Timers.T ...

  7. 使用System.Timers.Timer类实现程序定时执行

    在C#里关于定时器类有3个:System.Windows.Forms.Timer类.System.Threading.Timer类和System.Timers.Timer类. System.Windo ...

  8. C#学习常用类(1003)---Timer类(System.Timers.Timer)

    Timer类: 在设定的间隔之后生成事件,带有生成重复事件的选项. 若要浏览此类型的 .NET Framework 源代码,请参阅引用源. 命名空间:                   System ...

  9. [C#]System.Timers.Timer

    摘要 在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架 ...

  10. 在System.Timers.Timer的处理程序中调用MessageBox.Show()弹出的确是非模态的

    MessageBox.Show()不应该都是模态的么?但是我在System.Timers.Timer的处理程序中调用MessageBox.Show()弹出的确是非模态的

最新文章

  1. python快速编程入门课后简答题答案-Python编程:从入门到实践(课后习题8)
  2. 【Python】JupyterLab 出 Windows 桌面版了!
  3. python中常用的方法
  4. ng bind html 无效,angularjs中ng-bind-html的用法总结
  5. swing中如何将jtable中的数据导入到excel中?
  6. 自由软件之父回归 FSF,遭 1933 人、21 家组织联名抵制!
  7. 螺旋数字的python实现
  8. 【学习笔记】人体姿态识别
  9. JavaScript是什么?看着一篇就够了
  10. BurpSuite+Proxifer 抓取PC客户端HTTP数据包
  11. SylixOS -- 双网卡冗余备份使用说明
  12. paypal标准支付流程图
  13. 去除浏览器的hao123导航主页绑定
  14. 用户验收测试要求目标
  15. Redis学习之scard命令
  16. Clickhouse 分析分布式表的各类指标
  17. 李健《Django 2.0入门与实践》书中错误及修改方法
  18. 关于使用微软拼音在Hbuilder打不出顿号、的问题
  19. 条码软件如何制作渐变色文字
  20. 浅析云服务器常见的维护技巧

热门文章

  1. CodeForces 522D Closest Equals 树状数组
  2. Hive实现oracle的Minus函数
  3. CentOS下安装JDK6u30
  4. 线程和进程有什么区别(简单介绍)
  5. BZOJ 1901 洛谷 P2617 ZOJ 2112 Dynamic Rankings
  6. Net系列框架-Dapper+AutoFac 基于接口
  7. Java克隆--深克隆与浅克隆的区别
  8. 笔记:Java 性能优化权威指南 第5章 Java 应用性能分析
  9. can't init script for
  10. Win XP系统下局域网内无法访问其他计算机的共享如何解决