日期:2014-05-17 浏览次数:20439 次
using System;
using System.Threading;
public sealed class Program {
public static void Main() {
Console.WriteLine("Main thread: starting a timer");
Timer t = new Timer(ComputeBoundOp, 5, 0, 2000);
Console.WriteLine("Main thread: Doing other work here...");
Thread.Sleep(10000); //模拟其他工作(10秒钟)
t.Dispose(); //取消定时器
}
//该方法的签名必须与TimerCallback委托类型匹配
private static void ComputeBoundOp(Object state) {
//该方法由线程池中的线程执行
Console.WriteLine("In ComputeBoundOp: state={0}", state);
Thread.Sleep(1000);//模拟其他工作(1秒钟)
//在这个方法返回后,线程就回到线程池中,然后等待执行另一个任务
}
}