摘要

之前学习过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 = ;
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += timer_Elapsed;
Console.Read();
}
static int count = ;
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("第{0}次触发", count.ToString());
if (count == )
{
timer.Enabled = false;
}
Console.WriteLine("当前线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
System.Threading.Thread.Sleep();
Console.WriteLine("第{0}次处理完成", count.ToString());
count++; }
}
}

执行过程

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

System.Threading.Thread.Sleep();

可以看到这是,已经开始乱了,线程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 = ;
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += timer_Elapsed;
Console.Read();
}
static int count = ;
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (obj)
{
Console.WriteLine("第{0}次触发", count.ToString());
if (count == )
{
timer.Enabled = false;
}
Console.WriteLine("当前线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
System.Threading.Thread.Sleep();
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 == )
{
timer.Enabled = false;
}
Console.WriteLine("当前线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
System.Threading.Thread.Sleep();
Console.WriteLine("第{0}次处理完成", count.ToString());
count++;
isRunning = true;
} }

运行结果

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

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

执行结果

总结

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

[C#]System.Timers.Timer(2)的更多相关文章

  1. C# System.Timers.Timer的一些小问题?

    比如设置间隔时间是 1000msSystem.Timers.Timer mytimer = new System.Timers.Timer(1000);问题若响应函数执行的时间超过了 1000 ms, ...

  2. [C#]System.Timers.Timer

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

  3. C# --System.Timers.Timer 定时方法

    注意Start() 注意要等Interval 时间间隔 static void Main(string[] args) { System.Timers.Timer t = new System.Tim ...

  4. System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)

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

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

    使用System.Timers.Timer类实现程序定时执行 在C#里关于定时器类有3个:System.Windows.Forms.Timer类.System.Threading.Timer类和Sys ...

  6. .NET System.Timers.Timer的原理和使用(开发定时执行程序)

    概述(来自MSDN) Timer 组件是基于服务器的计时器,它使您能够指定在应用程序中引发Elapsed 事件的周期性间隔.然后可以操控此事件以提供定期处理.例如,假设您有一台关键性服务器,必须每周7 ...

  7. C# 定时器-System.Timers.Timer

    using Newtonsoft.Json; using Rafy; using Rafy.Domain; using System; using System.Collections.Generic ...

  8. .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用

    Windows Service(服务)  是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...

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

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

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

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

随机推荐

  1. C#为什么不能像C/C++一样的支持函数只读传参

    C#为什么不能像C/C++一样的支持函数只读传参? 这个问题其实问的人挺多的,我自己也经常想实现这个功能,但是发现总是那么的不尽人意. 有些人倒是给出了一下答案,但是都不能很好的解决像C/C++一样的 ...

  2. dotNet Core WEB程序使用 Nginx反向代理

    之前记录过一篇 使用 jexus 作为dotNetCore的反向代理,发现jexus的内存占用较大,最终选择使用Nginx的原因就是占用内存较小,以及性能较优(https://www.cnblogs. ...

  3. 真实项目中VS2015中自建T4模板生成文件的使用

    有可能许多小伙伴们发现,vs2015和2012的自带T4模板中的.tt文件改变非常之多,如果仅仅copyEF系统自己生成的模板文件,那可累了.以下是我自己整理的在2012和2015中都可以试用的代码. ...

  4. Linux Shell常用脚本整理

    轮询检测Apache状态并启用钉钉报警◆ #!/bin/bash shell_user="root" shell_domain="apache" shell_l ...

  5. 简单理解jQuery中$.getJSON、$.get、$.post、$.ajax用法

    在WEB开发中异步请求方式普遍使用,ajax技术减少程序员的工作量,也提升用户交互体验.AJAX的四种异步请求方式都能实现基本需求,闲话不多说,直接切入正题. 1.$.getJSON $.getJSO ...

  6. [Leetcode]123.买卖股票的最佳时机3

    [原题链接][https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/] 分析:动态规划+二分法.以第i天为分界线,计 ...

  7. [原创]K8 DNN密码解密工具(DotNetNuke Password Decrypt)

    工具: K8_DNN_Password_Decrypt编译: VS2012  C# (.NET Framework v2.0)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http: ...

  8. ubuntu图形化配置

    安装图形界面 apt-get install ubuntu-desktop 配置用户目录 udo useradd -s /bin/bash -d /home/wykeinstein -m wykein ...

  9. vuejs之Vue Devtools

    Vue Devtools大法好 这是一篇小白friendly教程 Vue Devtools是一款谷歌浏览器插件,专门为调试vue而设计.假设你做了一个vue应用,当你在调试的过程中,打开的控制台是这样 ...

  10. Android _关于fragment切换重新加载的解决分享给大家

    在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment但是,这样会有一个问题 ,应该很多朋友都遇到过:每次切换的时候,Fragment都会重新实例化,也就是运行 ...