例如,我的程序中有一个运行计时器功能的线程
Thread PopMonitoringThread = new Thread(new ThreadStart(PopMonitoring));
PopMonitoringThread.Start();
public static void PopMonitoring()
{
TimerCallback callback = new TimerCallback(Tick);
Timer stateTimer = new Timer(callback, null, 0, 1000);
}
//Timer method
static public void Tick(Object stateInfo)
{
try
{
if (Properties.Settings.Default.BatchingMode > 0)
{
if (batchTime.Subtract(DateTime.Now) < TimeSpan.Zero)
{
batchTime = DateTime.Now.AddMinutes(Properties.Settings.Default.BatchingMode);
Console.WriteLine("-----------------------------------------------------");
Process();
Console.WriteLine("Batch Process Run");
Console.WriteLine("-----------------------------------------------------");
}
Console.WriteLine("{0}", DateTime.Now.ToString("h:mm:ss"));
}
Console.WriteLine("Pop3 Monitoring start after: {0}", batchTime.Subtract(DateTime.Now));
}
catch (Exception e)
{
throw e;
}
}
当我注释掉我的 Process() 方法时,它每秒都可以正常工作,但是当我从 Tick 方法中取消注释 Process 方法时,计时器停止工作,即 Tick 方法停止工作。处理方法代码运行完美意味着没有编译和运行时错误。