前提:

  需要引入  System.Threading;

描述:

  在很多时间我们都需要进行延迟执行,或是定时执行一些指定业务,这个时候使用 Timer 是最合适的,而且 Timer 是Cpu 级别处理对系统影响很少,就算创建上千上万个 Timer 也不会影响。

  故见意多使用 Timer 是一个很好的定时任务器。

代码:

using System;
using System.Threading; namespace MyTimer
{
class Program
{
//构建 Timer
static Timer timer = new Timer(TimerCallBack, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
static void Main(string[] args)
{
//立即执行一次
timer.Change(TimeSpan.Zero, Timeout.InfiniteTimeSpan);
Console.ReadKey();
} static void TimerCallBack(object state)
{
var nextTime = DateTime.Now.AddSeconds(10);
Console.WriteLine("{0} 执行一次,下次执行时间 {1}", DateTime.Now, nextTime);
//执行完后,重新设置定时器下次执行时间.
timer.Change(nextTime.Subtract(DateTime.Now), Timeout.InfiniteTimeSpan);
}
}
}

  

  

C# System.Threading.Timer 定时器的更多相关文章

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

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

  2. System.Threading.Timer定时器使用注意事项

    1.定时器不要直接在方法里面定义和赋值,因为方法执行完,方法体内的变量会被GC回收. 有时候我们将timer定义在了方法里面,然后看到timer被执行了几次之后才失效,原因就是GC不一定会立即回收. ...

  3. 定时器:Timer:System.Threading.Timer类(转)

    最近的一个项目有一些地方需要用到定时功能,在设计过程中,突然发现.net的Timer类居然还有很多我以前没有用过的功能,这里就跟大家分享一下 注:这里的Timer类特指System.Threading ...

  4. System.Threading.Timer的使用技巧

    转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...

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

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

  6. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

    System.Windows.Forms.Timer.System.Timers.Timer.System.Threading.Timer的 区别和用法http://space.itpub.net/1 ...

  7. C# System.Threading.Timer 使用方法

    public class TimerHelper { System.Threading.Timer timer; public TaskSendMMS tasksendmms { get; set; ...

  8. System.Threading.Timer使用心得

    System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高. "只要在使用 Timer,就必须保留对它的引用."对于任何托 ...

  9. System.Threading.Timer 使用

    //定义计时器执行完成后的回调函数 TimerCallback timecallback = new TimerCallback(WriteMsg); //定义计时器 System.Threading ...

随机推荐

  1. redis和phpredis扩展的安装

    redis的安装https://code.google.com/p/redis/downloads/list下载redisredis-2.6.13.tar.gztar -xvzf redis-2.6. ...

  2. 杂项-公司:联邦快递百科-un

    ylbtech-杂项-公司:联邦快递百科 联邦快递( FedEx)是一家国际性速递集团,提供隔夜快递.地面快递.重型货物运送.文件复印及物流服务,总部设于美国田纳西州,隶属于美国联邦快递集团(FedE ...

  3. Java-Maven-Runoob:Maven 项目文档

    ylbtech-Java-Maven-Runoob:Maven 项目文档 1.返回顶部 1. Maven 项目文档 本章节我们主要学习如何创建 Maven 项目文档. 比如我们在 C:/MVN 目录下 ...

  4. 1078 Hashing

    题意:给出表长和待插入的元素,求每个元素的插入位置,散列函数为H(key)=key%TSize,解决冲突利用平方探测法(只考虑正向偏移). 思路:同1145 Hashing - Average Sea ...

  5. C#使用自定义字体

    /// <summary> /// 设置字体 /// </summary> /// <param name="path">字体文件路径,包含字体 ...

  6. 010. VS2015创建MVC项目

    1. 文件→新建→项目 2.选择空模板→选中MVC 3. 预览(如果是使用aspx, 则可以删除Views中的web.config, 如果使用Razor则不要删除, 否则会报Views/Home/In ...

  7. Sql Server优化之路

    本文只限coder级别层次上对Sql Server的优化处理简结,为防止专业DB人士有恶心.反胃等现象,请提前关闭此页面. 首先得有一个测试库,使用数据生成计划生成测试数据库(参考:http://de ...

  8. gevent异步,io自动切换

    #!/usr/bin/env python # encoding: utf-8  # Date: 2018/6/19 # # from gevent import monkey  # 这俩行必须放在首 ...

  9. js中的requestAnimationFrame

    js中的requestAnimationFrame requestAnimationFrame的作用就是重绘 一个简单的demo如下 <!DOCTYPE html> <html la ...

  10. 详解调试Apache的mod_rewrite模块

    大家都知道Apache里面的Rewrite规则是一件很蛋疼的事情,有时候只是想做一个伪静态而已,不想去研究那些复杂的规则,可官方给的规则又常常出错,出了问题我们就要调试一下,看看提交的参数被映射到了哪 ...