title: Is it possible to stop a Timer (System.Timers.Timer) from inside the OnTimedEvent Method
body: <p>I am trying to make a countdown function which would stop when the timer reaches 0. What is happening so far is that when the timer reaches 0 (and I haven't pressed any key) it keeps spamming me with the default message and it doesn't stop until I click a button.</p>
  <p>I would like to insert some code inside <code>OnTimedEvent</code> which would stop the timer (once it hits zero, display the message and stop spamming me). I have tryed multiple variations of <code>aTimer.Stop</code> <code>aTimer.Enabled = false;</code> etc.</p>
  <p>I get an error</p>
  <blockquote>
  <p><em>The name 'aTimer' does not exist in the current context.</em></p>
  </blockquote>
  <p><strong>Code:</strong></p>
  <pre>using System;
  using System.Timers;
  
  namespace MyProject
  {
      class Program
      {
          static void Main(string[] args)
          {
              Console.WriteLine(&quot;Main thread still running&quot;);
              Console.WriteLine(&quot;Press X to save the world.&quot;);
              Console.WriteLine(&quot;You have 10 SECONDS!&quot;);
  
              System.Timers.Timer aTimer = new System.Timers.Timer(1000);
              aTimer.Elapsed += OnTimedEvent;
              aTimer.Enabled = true;
  
  
              ConsoleKeyInfo input = Console.ReadKey(true);
  
              switch (input.KeyChar)
              {
                  default:
                      Console.WriteLine(&quot;Blah blah&quot;);
                      aTimer.Enabled = false;
                      break;
              }
  
              Console.ReadKey();
          }
  
          private static int _countDown = 10; // Seconds
          private static bool dMade = false;
  
          static void OnTimedEvent(object source, ElapsedEventArgs e)
          {
              if (dMade == false)
              {
                  if (_countDown-- &lt;= 0)
                  {
                      Console.WriteLine(&quot;You have doomed us all! DOOMED US ALL I TELL YOU!!!&quot;);
                  }
                  else
                  {
                      Console.CursorLeft = 0;
                      Console.ForegroundColor
                          = ConsoleColor.Red;
                      Console.Write(_countDown + &quot; &quot;);
                      Console.ForegroundColor
                          = ConsoleColor.Gray;
                  }
              }
              else
              {
                  Console.WriteLine(&quot;Success! The world is saved. Congrats!&quot;);
              }
          }
  </pre>