整个例子给你看看,应该可以解决你的问题using System; using System.Threading;namespace TestThreadPool { class TimerExampleState { public int counter; public Timer tmr; } class App { private static int _counter=0; private static Timer _tmr; public static void Main() { //TimerExampleState s=new TimerExampleState(); // Create the delegate that invokes methods for the timer. TimerCallback timerDelegate=new TimerCallback(CheckStatus);
// Create a timer that waits one second, then invokes every second. _tmr=new Timer(timerDelegate,null,1000,1000); // Keep a handle to the timer, so it can be disposed. //s.tmr=timer; while(_tmr!=null) Thread.Sleep(0); Console.WriteLine("Timer example done."); Console.ReadLine(); } static void CheckStatus(object state) { //TimerExampleState s=(TimerExampleState)state; _counter++; Console.WriteLine("{0} Checking Status {1}",DateTime.Now.TimeOfDay,_counter); if(_counter==5) { _tmr.Change(10000,100); Console.WriteLine("Changed..."); } if(_counter==10) { Console.WriteLine("Disposed"); _tmr.Dispose(); _tmr=null; } } } }
你用的是那个命名空间啊!
System.Threading.Timer 这个试试
using System.Threading;namespace TestThreadPool
{
class TimerExampleState
{
public int counter;
public Timer tmr;
}
class App
{
private static int _counter=0;
private static Timer _tmr; public static void Main()
{
//TimerExampleState s=new TimerExampleState(); // Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate=new TimerCallback(CheckStatus);
// Create a timer that waits one second, then invokes every second.
_tmr=new Timer(timerDelegate,null,1000,1000); // Keep a handle to the timer, so it can be disposed.
//s.tmr=timer;
while(_tmr!=null)
Thread.Sleep(0);
Console.WriteLine("Timer example done.");
Console.ReadLine();
}
static void CheckStatus(object state)
{
//TimerExampleState s=(TimerExampleState)state;
_counter++;
Console.WriteLine("{0} Checking Status {1}",DateTime.Now.TimeOfDay,_counter);
if(_counter==5)
{
_tmr.Change(10000,100);
Console.WriteLine("Changed...");
}
if(_counter==10)
{
Console.WriteLine("Disposed");
_tmr.Dispose();
_tmr=null;
}
}
}
}