如何在C#中只提高1个Timer事件? [英] How to raise only 1 Timer event in C#?

查看:173
本文介绍了如何在C#中只提高1个Timer事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何一次定时启动一个定时器事件。
例如,我有一个定时器,每10分钟会引发一个事件。
引发的事件需要10分钟或更长时间才能完成执行。
我希望计时器在事件结束后复位。
换句话说,我不想在任何一个时间提出超过一个事件的实例。

How do I get a timer event to fire one at a time. For example I have a timer that raises an event every 10 minutes. The event that is raised takes 10 or more minutes to finish executing. I would like the timer to reset AFTER the event has finished. In other words I do not want to raise more than 1 instance of the event at any one time.

推荐答案

通常我做的是让事件停止计时器的提醒,然后在事件进程完成时重新启动计时器:

Usually what I do is have my event stop the timer when it's raised and then restart the timer when the event process completes:

private void timerHandler(object sender, TimerElapsedEventArgs e)
{
    Timer timer = (Timer)sender;
    timer.Stop();
    RunProcess();
    timer.Start();
}

public void RunProcess()
{
    /* Do stuff that takes longer than my timer interval */
}

现在我的计时器将在流程完成后再次启动

Now my timer will start again on completion of the process

这篇关于如何在C#中只提高1个Timer事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆