繁体   English   中英

重置倒数计时器

[英]Reset CountDown Timer

没有 EventHandler (timerIdle_Tick),计时器不会启动:

timerIdle.Tick += new EventHandler(timerIdle_Tick);

按照您的指示后的计时器代码:

class Timers
{
    public static System.Windows.Forms.Label lblIdle;
    public static Timer timerIdle = new Timer();
    public static int segundo = 0;

    // TIMER IDLE
    public static void timerIdleOn()
    {
        segundo = 300;
        timerIdle.Interval = 1000;
        timerIdle.Start();
    }
    public static void timerIdleOff()
    {
        timerIdle.Stop();
    }

    public static void timerIdle_Tick(object sender, EventArgs e)
    {
        segundo--;

        if (segundo == 0)
        {
            timerIdle.Stop();
            dgView.addLinha(2, "config_Teste", "TIMER ACABOU", 0);
        }
        else
            dgView.addLinha(2, "config_Teste", TimeSpan.FromSeconds(segundo).ToString(@"mm\:ss"), 0);
    }
}

它跟着定时器开始的地方,我不使用按钮,只是暂时测试方法,之后就不存在了。

namespace HC
{
    public partial class frm_console : Form
    {
        public frm_console()
        {
            InitializeComponent();
            dgView.DataConsole = this.DataConsol;           
            Timers.timerIdleOn();
        }
    }
}

对此:

timerIdle.Stop();
timerIdle.Start();

Timer object 对计时一无所知。 这只是一种允许您的timerIdle_Tick()方法定期调用的机制。 因此,很自然地,简单地再次停止和启动计时器对计时器的显示时间完全没有影响。

与您之前关于该程序的问题一样,您仍然没有提供最小、完整和可验证的代码示例 所以不可能确定需要什么。

但是查看您发布的代码,在我看来,您可以简单地为您的tempo变量分配一个新值,以便在您想要的任何时间重新开始计数。 例如:

timerIdle.Stop();
tempo = 300; // 300 seconds, or in other words, 5 minutes
timerIdle.Start();

对于它的价值,您的代码可以简化。 好吧,暂时忽略“在每个滴答声上递减一个秒计数器”只是实现这一点的错误方法(正如您在上一个问题中向您解释的那样),您的Tick处理程序方法的主体可能看起来更像这样并且仍然做同样的事情:

public static void timerIdle_Tick(object sender, EventArgs e)
{
    tempo--; // decrement first, then segundo and minuto already will be automatically decremented correctly

    // NOTE: it's not clear whether these are even used outside this method.
    // If not, just make them locals and do this computation only in the
    // "else" block below.
    minuto = tempo / 60; // will be 0 if tempo < 60
    segundo = tempo % 60; // will be tempo if tempo < 60

    if (tempo < 0)
    {
        timerIdle.Stop();

        // WARNING: this renders the timerIdle object invalid for future use.
        // If you try to restart the timer without creating a new one after this
        // statement executes, an exception will be thrown. If you intend to ever
        // reuse timerIdle, you should not dispose it here.
        timerIdle.Dispose();

        dgView.addLinha(2, "config_Teste", "TIMER ACABOU", 0);
    }
    else
    {
        // no need to recombine minuto and segundo back into tempo,
        // since tempo was already decremented to start with and
        // continues to have the right value here.

        string temp = string.Format($"{minuto:D2}:{segundo:D2}");
        lblIdle.Text = temp;
        dgView.addLinha(2, "config_Teste", temp, 0);
    }
}

这更像是我期望看到的。 计时器正在frm_console class 内部创建和处理。 请注意,所有static已被删除:

public partial class frm_console : Form
{

    public int segundo = 300;
    public Timer timerIdle = new Timer();   
    
    public frm_console()
    {
        InitializeComponent();
        dgView.DataConsole = this.DataConsol;
        timerIdle.Interval = 1000; 
        timerIdle.Tick += new EventHandler(timerIdle_Tick);        
        timerIdle.Start();
    }
    
    private void btnReset_Click(object sender, EventArgs e)
    {
        segundo = 300; // reset to 5 minutes
        timerIdle.Start();
    }

    private void btnPauseResume_Click(object sender, EventArgs e)
    {
        if (timerIdle.Enabled)
        {
            timerIdle.Stop();
        }
        else if (segundo > 0)
        {
            timerIdle.Start();
        }
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        timerIdle.Stop();
    }   

    public void timerIdle_Tick(object sender, EventArgs e)
    {
        segundo--;
        
        if (segundo == 0)
        {
            timerIdle.Stop();
            dgView.addLinha(2, "config_Teste", "TIMER ACABOU", 0);
        }
        else
        {
            dgView.addLinha(2, "config_Teste", TimeSpan.FromSeconds(segundo).ToString(@"mm\:ss"), 0);
        }
    }
            
}

以“btn”开头的方法是按钮单击处理程序,但它们可以转换为常规方法。 你应该能够理解他们在做什么。

你试过这个:

  timerIdle.Enabled = false;
  timerIdle.Enabled = true;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM