前提:

  需要引入  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. JS 获取鼠标坐标

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. java图形用户界面添加背景颜色不成功的解决方案

    总结:背景颜色不成功,那么使用这个方法试试.getContentpane(); package clientFrame; import java.awt.Color; import java.awt. ...

  3. mysql 无意重启 [Note] /usr/sbin/mysqld: Normal shutdown

    情况: 今早发现,昨天下午安装的4台mysql服务器,突然出现,由于在shell窗口 (root@localhost:mysql.sock) [(none)]> 190102 18:12:16 ...

  4. 移植OpenWrt到CuHead Pro WiFi

    移植OpenWrt到CuHead Pro WiFi Posted by: zou, baozhu , 三月 13, 2014 CuHead Pro是一款路由器开发板,下面是开发板的配置信息. 名称 型 ...

  5. 安卓端后台登录接口单元测试demo

    package com.js.ai.modules.pointwall.interfac; import java.io.IOException; import java.io.Unsupported ...

  6. 如何在 Internet Explorer 11中开启 WebGL

    原文地址:http://www.hiwebgl.com/?p=1210 国外开发者Francois Remy在泄露版Windows Blue附带的Internet Explorer 11中发现,Web ...

  7. 用Python语言设计GUI界面

    我们大家都编写过程序,但是如果能够设计一个GUI界面,会使程序增添一个很大的亮点!今天就让我们来用目前十分流行的python语言写出一个最基本的GUI,为日后设计更加漂亮的GUI打下基础. 工具/原料 ...

  8. python--open用法

    open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 如:' ...

  9. pl/sql的to_char和to_date

    今天同事让笔者将sql查询出来的时间转换为指定格式的字符串,笔者当时懵逼了,印象中记得有处理过,但就是一时想不起来了,等处理了这个问题后,笔者觉得有必要记录一下,毕竟不知道这个是很掉面子的事不是?好了 ...

  10. linux中memset的正确用法

    linux中memset的正确用法 [起因]希望对各种类型的数组进行初始化,避免野值 [函数头文件] 提示:在linux中可以在terminal中输入 "man memset"进行 ...