简体   繁体   中英

C#: How do I stop a timer in the middle of a tick

I set a timer in form1(windows form) to tick every second

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

In the timer.tick event, I set it to stop after a certain number of ticks.

 private void Timer_Tick(object sender, EventArgs e)
   {
     if(x == 0)
        {
           MessageBox.Show("ETC");
           Timer.Stop();
        }
  }

However, I found that the timer.Stop() was not ending the timer, and the message box continued popping up one time every second even after x = 0. Why is this? And how do I stop the timer?

Here is the whole code

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

And here is the tick event

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



   }

The main problem is at this part

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

EVen when numRepeat is 1 and current = 0, the timer does not stop even when I declared it(checkTiming.Stop())

According to the documentation , MessageBox :

Displays a message window, also known as a dialog box, which presents a message to the user. It is a modal window, blocking other actions in the application until the user closes it.

This means that when control reaches the MessageBox.Show line, it stops there until the user closes the message box. This means that Timer.Stop will not be called until the user closes the message box. This is why the timer will still tick.

To fix this, just change the order of the method calls:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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