-2

Im new to programming in C# and i encountered some problems, I am trying to create a countdown Console Command but im stuck on how to terminate the loop using a user input (in this case, when the user presses the "Enter" Button)

Here is the code that I currently have

using System;
using System.Threading;

class stopWatch {
  public static void Main (string[] args) {
    Console.WriteLine ("Access The Clock? Y/N");
    string yN = Console.ReadLine();
    if ((yN == "y") || (yN == "Y")) {
      Console.WriteLine ("Timer (T) or Stopwatch (S)?");
      var sT = Console.ReadLine();
      if ((sT == "s") || (sT == "S")) {
        Console.WriteLine ("Press the 'Enter' Button to Start");
        Console.ReadLine();
        Console.WriteLine("Stopwatch Started");
        Console.WriteLine("Press the 'Enter' Button again to Stop");
        for(int i = 0; i >= 0; i ++) {
          Console.WriteLine(i);
          Thread.Sleep(1000);
        }
      }
    }
    else if ((yN =="n") || (yN == "N")) {
      Console.WriteLine ("Alright, Thank You");
      Environment.Exit(0);
    }
    else {
      Console.WriteLine ("Wrong Input");
      Environment.Exit (0);
    }
  }
}

keep in mind im very new to c# and loops, i'm getting a hard time translating and trying on everything, it'd be really grateful of me to get an extra explanation for your answers

2
  • Have you considered using a while loop? Commented Oct 14, 2020 at 14:54
  • There isn't a loop in the code posted... Commented Oct 14, 2020 at 14:57

2 Answers 2

0

You need to use separate thread for that. Threads is advanced theme, but the basic goal is to do several things at once (in your example run stopwatch and wait for user input). Here is full working code based on your sample:

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp13
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Access The Clock? Y/N");
            string yN = Console.ReadLine().ToLower();

            if (yN == "y")
            {
                Console.WriteLine("Timer (T) or Stopwatch (S)?");
                var sT = Console.ReadLine().ToLower();

                if (sT == "s")
                {
                    Console.WriteLine("Press the 'Enter' Button to Start");
                    Console.ReadLine();

                    bool runStopWatch = true;
                    Task.Run(() =>
                    {
                        for (int i = 0;; i++)
                        {
                            Console.WriteLine(i);
                            Thread.Sleep(1000);

                            if (!runStopWatch)
                            {
                                break;
                            }
                        }
                    });
                    Console.WriteLine("Stopwatch Started");
                    Console.WriteLine("Press the 'Enter' Button again to Stop");
                    Console.ReadLine();
                    runStopWatch = false;
                }
            }
            else if (yN == "n")
            {
                Console.WriteLine("Alright, Thank You");
                Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("Wrong Input");
                Environment.Exit(0);
            }
        }
    }
}

Task.Run starts a new thread, so your stopwatch runs independent of the main thread (which waits for user to press Enter). Variable runStopWatch is shared between threads (don't think about it right now), so when main thread sets it to false, stopwatch's thread sees it and terminated the loop.

Side mote. More proper way to do this is to use CancellationToken istead of bool variable. But again. don't worry about it right now.

Sign up to request clarification or add additional context in comments.

2 Comments

If I may ask, why did you use Boolean there?
To communicate information between threads. Could you please ask more specific question? What don't you understand about that boolean variable?
0

Since you're new im going to try to explain it as simple as possible. The solution you're trying to create is a solution to a multi-threaded problem. This means you will have to do an action while another action takes place. This can be resolved using async methods, this might be too advanced for now, so I will suggest reading up on loops and then multi-threaded programming first.

But for an answer i will have to refer to this post here on how to fix this particular problem. (Look at the second part of the accepted answer).

1 Comment

Thank you so much for your help :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.