C# System.Threading.Timer
提供以指定的时间间隔对线程池线程执行方法的机制
using System;
using System.Threading; class TimerExample
{
static void Main()
{
// Create an AutoResetEvent to signal the timeout threshold in the
// timer callback has been reached.
var autoEvent = new AutoResetEvent(false); var statusChecker = new StatusChecker(); // Create a timer that invokes CheckStatus after one second,
// and every 1/4 second thereafter.
Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n",
DateTime.Now);
var stateTimer = new Timer(statusChecker.CheckStatus,
autoEvent, , ); // When autoEvent signals, change the period to every half second.
autoEvent.WaitOne();
stateTimer.Change(, );
Console.WriteLine("\nChanging period to .5 seconds.\n"); // When autoEvent signals the second time, dispose of the timer.
autoEvent.WaitOne();
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
}
} class StatusChecker
{
private int invokeCount;
private int maxCount; public StatusChecker(int count)
{
invokeCount = ;
maxCount = count;
} // This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString()); if(invokeCount == maxCount)
{
// Reset the counter and signal the waiting thread.
invokeCount = ;
autoEvent.Set();
}
}
}
// The example displays output like the following:
// 11:59:54.202 Creating timer.
//
// 11:59:55.217 Checking status 1.
// 11:59:55.466 Checking status 2.
// 11:59:55.716 Checking status 3.
// 11:59:55.968 Checking status 4.
// 11:59:56.218 Checking status 5.
// 11:59:56.470 Checking status 6.
// 11:59:56.722 Checking status 7.
// 11:59:56.972 Checking status 8.
// 11:59:57.223 Checking status 9.
// 11:59:57.473 Checking status 10.
//
// Changing period to .5 seconds.
//
// 11:59:57.474 Checking status 1.
// 11:59:57.976 Checking status 2.
// 11:59:58.476 Checking status 3.
// 11:59:58.977 Checking status 4.
// 11:59:59.477 Checking status 5.
// 11:59:59.977 Checking status 6.
// 12:00:00.478 Checking status 7.
// 12:00:00.980 Checking status 8.
// 12:00:01.481 Checking status 9.
// 12:00:01.981 Checking status 10.
//
// Destroying timer.
using System;
using System.Threading; class TimerExample
{
public static int loop_index = ;
public static AutoResetEvent autoEvent = new AutoResetEvent(false); static void Main()
{
int loop_count = ; // Create an AutoResetEvent to signal the timeout threshold in the
// timer callback has been reached. // Create a timer that invokes CheckStatus after one second,
// and every 1/4 second thereafter.
Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n",
DateTime.Now);
var stateTimer = new Timer(show,
autoEvent, , ); // When autoEvent signals, change the period to every half second.
autoEvent.WaitOne();
stateTimer.Change(, );
Console.WriteLine("\nChanging period to .5 seconds.\n"); // When autoEvent signals the second time, dispose of the timer.
autoEvent.WaitOne();
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
} public static void show(Object stateInfo)
{
Console.WriteLine("{0:h:mm:ss.fff} Run in Show.\n",
DateTime.Now);
Console.WriteLine(loop_index);
loop_index = loop_index + ;
if (loop_index > )
{
autoEvent.Set();
}
}
}
构造函数
| Timer(TimerCallback) | |
| Timer(TimerCallback, Object, Int32, Int32) |
使用 32 位的有符号整数指定时间间隔,初始化 |
| Timer(TimerCallback, Object, Int64, Int64) |
用 64 位有符号整数来度量时间间隔,以初始化 |
| Timer(TimerCallback, Object, TimeSpan, TimeSpan) |
初始化 |
| Timer(TimerCallback, Object, UInt32, UInt32) |
用 32 位无符号整数来度量时间间隔,以初始化 |
方法
| Change(Int32, Int32) |
更改计时器的启动时间和方法调用之间的间隔,用 32 位有符号整数度量时间间隔。 |
| Change(Int64, Int64) |
更改计时器的启动时间和方法调用之间的间隔,用 64 位有符号整数度量时间间隔。 |
| Change(TimeSpan, TimeSpan) |
更改计时器的启动时间和方法调用之间的时间间隔,使用 TimeSpan 值度量时间间隔。 |
| Change(UInt32, UInt32) |
更改计时器的启动时间和方法调用之间的间隔,用 32 位无符号整数度量时间间隔。 |
| CreateObjRef(Type) |
创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。 (Inherited from MarshalByRefObject) |
| Dispose() |
释放由 Timer 的当前实例使用的所有资源。 |
| Dispose(WaitHandle) |
释放 Timer 的当前实例使用的所有资源并在释放完计时器时发出信号。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (Inherited from Object) |
| GetHashCode() |
作为默认哈希函数。 (Inherited from Object) |
| GetLifetimeService() |
检索控制此实例的生存期策略的当前生存期服务对象。 (Inherited from MarshalByRefObject) |
| GetType() |
获取当前实例的 Type。 (Inherited from Object) |
| InitializeLifetimeService() |
获取生存期服务对象来控制此实例的生存期策略。 (Inherited from MarshalByRefObject) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。 (Inherited from Object) |
| MemberwiseClone(Boolean) |
创建当前 MarshalByRefObject 对象的浅表副本。 (Inherited from MarshalByRefObject) |
| ToString() |
返回表示当前对象的字符串。 (Inherited from Object) |
C# System.Threading.Timer的更多相关文章
- System.Threading.Timer 定时器的用法
System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此 .Net Framework 提供了5个重载的构造 ...
- 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 ...
- System.Threading.Timer的使用技巧
转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...
- System.Threading.Timer如何正确地被Dispose
System.Threading.Timer是.NET中一个定时触发事件处理方法的类(本文后面简称Timer),它背后依靠的是.NET的线程池(ThreadPool),所以当Timer在短时间内触发了 ...
- System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法
System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...
- System.Threading.Timer
GLog.WLog("_thdTimer before"); _thdTimer = new System.Threading.Timer(new TimerCallback(Ti ...
- 当时钟事件声明为过程变量 让system.threading.timer时钟失效
这个项目的小模块就是画label 控件到tablepayoutpanel表单 之中, 中间用到了时钟,事件(带返回值的),哈希表 .由于时钟定义在 form1的启动构造函数中导致了form1,启动完毕 ...
- c# 多线程之-- System.Threading Timer的使用
作用:每隔多久去执行线程里的方法. class ThreadTimerDemo { static void Main(string[] args) { // Create an AutoResetEv ...
随机推荐
- XXX系统项目目标文档课堂讨论
XXXX重大技术征集系统 1.讨论结果: 2.项目目标文档 A目标: 1. 实现普通用户在线需求填报,个人信息管理,需求结果查看. 2. 实现审核员用户的需求审核,需求查看浏览和生成图表结果. 3. ...
- There is no setter for property named 可能产生的原因!
There is no setter for property named 'operateIP ' in 'class com.chinaunicom.wsp.facade.entity.User ...
- Linux命令集
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- RIPng 配置
一.拓扑图 二.配置过程 1)首先将各个端口的IP地址按照如上图所示配置完毕 2)在全局模式下创建rip ipv6 router rip [name] 3)在各个路由器上进入所连接的端口启用RIPng ...
- java接口签名(Signature)实现方案
预祝大家国庆节快乐,赶快迎接美丽而快乐的假期吧!!! 一.前言 在为第三方系统提供接口的时候,肯定要考虑接口数据的安全问题,比如数据是否被篡改,数据是否已经过时,数据是否可以重复提交等问题.其中我认为 ...
- Adobe Premiere Pro生成峰值文件假死
一.正文 使用Adobe的Premiere Pro CC进行视频剪辑制作的时候,有的时候在右下角总会出现一个“自动生成峰值文件”的提示符,并跟随一个进度条: 大部分时候,这并不会引起什么问题.虽然我也 ...
- 把存储过程结果集SELECT INTO到临时表
把存储过程结果集SELECT INTO到临时表 在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO . 使用select into会自动生成临时表,不需要 ...
- php 通用数据库类
<?php // 数据库连接类 class DB{ //私有的属性 private static $dbcon=false; private $host; private $port; priv ...
- [P1082][NOIP2012] 同余方程 (扩展欧几里得/乘法逆元)
最近想学数论 刚好今天(初赛上午)智推了一个数论题 我屁颠屁颠地去学了乘法逆元 然后水掉了P3811 和 P2613 (zcy吊打集训队!)(逃 然后才开始做这题. 乘法逆元 乘法逆元的思路大致就是a ...
- yii2 basic版 MVC 部分
一.model 1.什么是模型 模型是 MVC 模式中的一部分,是表现业务数据.规则和逻辑的对象 可通过继承 yii\base\Model 或它的子类(activeRecord)来定义模型类,基类 y ...