C# C:如何在节拍中间停止计时器?

C# C:如何在节拍中间停止计时器?,c#,timer,C#,Timer,我在form1(windows窗体)中设置了一个计时器,使其每秒都滴答作响 private void Form1_Load(object sender, EventArgs e) { Timer.Interval = 1000; Timer.Start; } 在timer.tick事件中,我将其设置为在一定数量的tick之后停止 private void Timer_Tick(object sender, EventArgs e) {

我在form1(windows窗体)中设置了一个计时器,使其每秒都滴答作响

private void Form1_Load(object sender, EventArgs e)
    {
      Timer.Interval = 1000;
      Timer.Start;
    }
在timer.tick事件中,我将其设置为在一定数量的tick之后停止

 private void Timer_Tick(object sender, EventArgs e)
   {
     if(x == 0)
        {
           MessageBox.Show("ETC");
           Timer.Stop();
        }
  }
但是,我发现timer.Stop()并没有结束计时器,即使在x=0之后,消息框仍会每秒弹出一次。为什么会这样?我该如何停止计时器

这是全部代码

 private void btnStart_Click(object sender, EventArgs e)
    {
        checkTiming.Stop();

        try
        {
            timeTick.hour = int.Parse(textBoxHour.Text);
            timeTick.min = int.Parse(textBoxMin.Text);
            timeTick.sec = int.Parse(textBoxSec.Text);
            numRepeat = int.Parse(textBoxRepeat.Text);
            timeTick.totalTime = 0;
            current = timeTick.hour * 3600 + timeTick.min * 60 + timeTick.sec;

            if (timeTick.hour * 3600 + timeTick.min * 60 + timeTick.sec > 0 && timeTick.min <= 60 && timeTick.sec <=60 )
            {

            memory.listHistory.Add(memory.padMultipleString(textBoxHour.Text, textBoxMin.Text, textBoxSec.Text));
            updateListViewHistory(memory.listHistory);

            checkTiming.Interval = 1000;
            checkTiming.Start();      
            }

            else 
            {
                MessageBox.Show("Please enter a valid value", "Error", MessageBoxButtons.OK);
                initialise();
            }
        }

        catch
        {
            MessageBox.Show("Please enter positive integers to all textBoxes", "Error", MessageBoxButtons.OK);
            initialise();
        }
    }
主要问题在这一部分

       private void checkTiming_Tick(object sender, EventArgs e)
   {

       timeTick.updateTime(current);
       updateTextBoxes();

       if(current > 0)
       {
           current--;
       }

       if(current == 0)
       {
          if(numRepeat > 1)
          {
              numRepeat--;
              current = timeTick.totalTime;
              //Console.WriteLine(current);

              MessageBox.Show(memory.listHistory.Last() + " has elapsed. " + "Repeating " + numRepeat.ToString() + " more times", "Timing has Ended", MessageBoxButtons.OK);
          }

           if(numRepeat == 1)
           {
               MessageBox.Show(memory.listHistory.Last() + " has elapsed", "Timing has Ended", MessageBoxButtons.OK);
               timeTick.totalTime = 0;
               Console.WriteLine(numRepeat);
               checkTiming.Stop();
           }
       }
即使numRepeat为1且current=0,计时器也不会停止,即使我声明了它(checkTimeing.stop())

根据,
消息框:

显示消息窗口,也称为对话框,向用户显示消息。它是一个模式窗口,阻止应用程序中的其他操作,直到用户关闭它

这意味着,当控件到达
MessageBox.Show
行时,它会在那里停止,直到用户关闭消息框。这意味着在用户关闭消息框之前,
Timer.Stop
不会被调用。这就是计时器仍会滴答作响的原因

要解决此问题,只需更改方法调用的顺序:

   checkTiming.Stop();
   MessageBox.Show(memory.listHistory.Last() + " has elapsed", "Timing has Ended", MessageBoxButtons.OK);
   timeTick.totalTime = 0;
   Console.WriteLine(numRepeat);

这里的x是什么?x的值是否改变?您需要为我们提供一个解决方案。这意味着我们可以复制、粘贴和运行您的代码,并亲自查看您的问题。我们无法使用您发布的代码执行此操作。您是在同一表单中终止计时器,还是在另一表单中终止计时器?请在显示消息框之前停止计时器。MessageBox.Show将阻止并泵送窗口消息,这将允许计时器再次启动。这并不是对您的问题的回答,但您应该学会避免使用
int.Parse
,而是使用
int.TryParse
。然后,您可以摆脱糟糕的
代码块,尝试
/
捕获
代码块,编写更清晰的代码。
   checkTiming.Stop();
   MessageBox.Show(memory.listHistory.Last() + " has elapsed", "Timing has Ended", MessageBoxButtons.OK);
   timeTick.totalTime = 0;
   Console.WriteLine(numRepeat);