title: C#: How do I stop a timer in the middle of a tick
body: <p>I set a timer in form1(windows form) to tick every second</p>
  
  <pre><code>private void Form1_Load(object sender, EventArgs e)
      {
        Timer.Interval = 1000;
        Timer.Start;
      }
  </code></pre>
  
  <p>In the timer.tick event, I set it to stop after a certain number of ticks.</p>
  
  <pre><code> private void Timer_Tick(object sender, EventArgs e)
     {
       if(x == 0)
          {
             MessageBox.Show("ETC");
             Timer.Stop();
          }
    }
  </code></pre>
  
  <p>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?</p>
  
  <p>Here is the whole code</p>
  
  <pre><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 &gt; 0 &amp;&amp; timeTick.min &lt;= 60 &amp;&amp; timeTick.sec &lt;=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();
          }
      }
  </code></pre>
  
  <p>And here is the tick event</p>
  
  <pre><code> private void checkTiming_Tick(object sender, EventArgs e)
     {
  
         timeTick.updateTime(current);
         updateTextBoxes();
  
         if(current &gt; 0)
         {
             current--;
         }
  
         if(current == 0)
         {
            if(numRepeat &gt; 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();
             }
         }
  
  
  
     }
  </code></pre>
  
  <p>The main problem is at this part</p>
  
  <pre><code>       private void checkTiming_Tick(object sender, EventArgs e)
     {
  
         timeTick.updateTime(current);
         updateTextBoxes();
  
         if(current &gt; 0)
         {
             current--;
         }
  
         if(current == 0)
         {
            if(numRepeat &gt; 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();
             }
         }
  </code></pre>
  
  <p>EVen when numRepeat  is 1 and current = 0, the timer does not stop even when I declared it(checkTiming.Stop())</p>