0
votes

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.

I would like to insert some code inside OnTimedEvent which would stop the timer (once it hits zero, display the message and stop spamming me). I have tryed multiple variations of aTimer.Stop aTimer.Enabled = false; etc.

I get an error

The name 'aTimer' does not exist in the current context.

Code:

using System;
using System.Timers;

namespace MyProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main thread still running");
            Console.WriteLine("Press X to save the world.");
            Console.WriteLine("You have 10 SECONDS!");

            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("Blah blah");
                    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-- <= 0)
                {
                    Console.WriteLine("You have doomed us all! DOOMED US ALL I TELL YOU!!!");
                }
                else
                {
                    Console.CursorLeft = 0;
                    Console.ForegroundColor
                        = ConsoleColor.Red;
                    Console.Write(_countDown + " ");
                    Console.ForegroundColor
                        = ConsoleColor.Gray;
                }
            }
            else
            {
                Console.WriteLine("Success! The world is saved. Congrats!");
            }
        }
2

2 Answers

2
votes

I get an error The name 'aTimer' does not exist in the current context.

This is because the aTimer is not in scope in the OnTimedEvent method. The scope of the aTimer is in the Main method because that is where it is declared. If you move the aTimer to a static field inside the Program class instead of a local variable it will be in scope for all the methods in it:

class Program
    {
        static System.Timers.Timer aTimer;
        static void Main(string[] args)
        {
            Console.WriteLine("Main thread still running");
            Console.WriteLine("Press X to save the world.");
            Console.WriteLine("You have 10 SECONDS!");

            aTimer = new System.Timers.Timer(1000);
            aTimer.Elapsed += OnTimedEvent;
            aTimer.Enabled = true;

...

Then you can reference aTimer in your OnTimedEvent method.

static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
           aTimer.Enabled = false;
...

A variables scope depends on where it is declared, so if it's declared in the body of the class, all methods can access it (if it's static or the methods are not static). If a variable is declared in the scope of a method, it's only visible to that method (from the point it's declared up until the closing brace '}' for that method.

3
votes

In OnTimedEvent, the object source is your timer. You can stop it with the following line:

((System.Timers.Timer) source).Enabled = false;