C#:如何在刻度中间停止计时器

时间:2018-06-23 05:29:49

标签: c# timer

我在form1(Windows窗体)中设置了一个计时器,以秒为单位计时

private void Form1_Load(object sender, EventArgs e)
    {
      Timer.Interval = 1000;
      Timer.Start;
    }

在timer.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();
           }
       }



   }

主要问题在这部分

       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时,即使我声明了它(checkTiming.Stop()),计时器也不会停止

1 个答案:

答案 0 :(得分:4)

根据documentationMessageBox

  

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

这意味着当控件到达MessageBox.Show行时,它将停在那里,直到用户关闭消息框。这意味着Timer.Stop直到用户关闭消息框后才会被调用。这就是计时器仍会计时的原因。

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

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