.net中定时器超时问题

2021-03-02 14:27

阅读:729

标签:exception   一个   report   test   方法   rtp   complete   +=   run   

当定时器下一个周期到来的时候,上一个周期程序还没有执行完,这种情况需要在第一个周期进入的时候设置timer enabled为false,等执行完成之后再设为true;

 

main()

{

try

{
m_timer = new System.Timers.Timer();
m_timer.Interval = 30000;
m_timer.Elapsed += new ElapsedEventHandler(Timer_Tick);
m_timer.Enabled = true;
}
catch (Exception ex)
{
LogManager.WriteStatusLine("创建监听Socket线程失败...");
}

}

private void Timer_Tick(object sender, ElapsedEventArgs e)
{
m_timer.Enabled = false; //先关闭定时器,完成一次循环后重新开启--在RunWorkerCompleted方法中
BackgroundWorker work = new BackgroundWorker();
work.WorkerReportsProgress = true;
work.WorkerSupportsCancellation = true;
work.DoWork += new DoWorkEventHandler(RunTaskWork);
work.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
work.RunWorkerAsync();
}

private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
outLog.Info("定时器轮询周期结束");
m_timer.Enabled = true;
}

private void RunTaskWork(object sender, DoWorkEventArgs e)
{
outLog.Info("定时器开始新轮询周期");
StartPost();
}

 

.net中定时器超时问题

标签:exception   一个   report   test   方法   rtp   complete   +=   run   

原文地址:https://www.cnblogs.com/zxf12107/p/14334080.html


评论


亲,登录后才可以留言!