提供以指定的时间间隔对线程池线程执行方法的机制
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) |