C# System.Threading.Timer 定时器
前提:
需要引入 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 定时器的更多相关文章
- System.Threading.Timer 定时器的用法
System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此 .Net Framework 提供了5个重载的构造 ...
- System.Threading.Timer定时器使用注意事项
1.定时器不要直接在方法里面定义和赋值,因为方法执行完,方法体内的变量会被GC回收. 有时候我们将timer定义在了方法里面,然后看到timer被执行了几次之后才失效,原因就是GC不一定会立即回收. ...
- 定时器:Timer:System.Threading.Timer类(转)
最近的一个项目有一些地方需要用到定时功能,在设计过程中,突然发现.net的Timer类居然还有很多我以前没有用过的功能,这里就跟大家分享一下 注:这里的Timer类特指System.Threading ...
- System.Threading.Timer的使用技巧
转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- 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 ...
- C# System.Threading.Timer 使用方法
public class TimerHelper { System.Threading.Timer timer; public TaskSendMMS tasksendmms { get; set; ...
- System.Threading.Timer使用心得
System.Threading.Timer 是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高. "只要在使用 Timer,就必须保留对它的引用."对于任何托 ...
- System.Threading.Timer 使用
//定义计时器执行完成后的回调函数 TimerCallback timecallback = new TimerCallback(WriteMsg); //定义计时器 System.Threading ...
随机推荐
- Java [parms/options] range -b 100 -c 10 -i 100 -t 300 -s 180
3 down vote Just run the command java -X and you will get ans of all_ C:\Users\Admin>java -X -Xmi ...
- TI技术官方论坛
https://e2echina.ti.com/question_answer/dsp_arm/c6000_dsp/f/32/t/172279
- 1100 Mars Numbers
题意:进制转换. 思路:注意当数字是13的倍数时,只需高位叫法的单词.比如26,是“hel”,而不是“hel tret”.我被坑在这里了!对应语句1的处理.另外,在输入n和n个字符串之间需要一个吸收字 ...
- Python——Dict
Python字典(Dictionary) 字典是一种可变容器模型,可存储任意类型对象. 字典的每个键值(key => value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花 ...
- linux下FTP使用
如何在linux下开启FTP服务 1. 首先服务器要安装ftp软件,查看是否已经安装ftp软件下: #which vsftpd 如果看到有vsftpd的目录说明服务器已经安装了ftp软件 ...
- 英语单词state和status的区别
state:比较常用,各种状态都可以用它,但是它更着重于一种心理状态或者物理状态. Status:用在人的身上一般是其身份和地位,作"状态,情形"讲时,多指政治和商业. state ...
- C#使用ADO操作Excel
1 说明 把excel当成一个数据库,类似于Access数据库来操作. 2 源代码 2.1 Model层 /// <summary> /// 人员信息 /// </summary&g ...
- cmd命令删除文件及文件夹
rmdir /s/q wenjianming 其中: /s 是代表删除所有子目录跟其中的档案. /q 是不要它在删除档案或目录时,不再问我 Yes or No 的动作.
- 国庆前执行更新承诺SO交期 FP_SO2SAP
每年9月20日到30号执行以下程序:创建日期为昨天的订单,且承诺交期为10月1到3号,则承诺交期需加7天:创建日期为昨天的订单, 承诺交期为4号到11月1日,承诺交期需加4天 存储过程:FP_SO2S ...
- 粗粒度(Coarse-grained)vs细粒度(fine-grained)
在读的一篇文献中关于RDF的描述: As we know, RDF data is a set of triples with the form (subject, property, object) ...