0

Hi I am trying to make a console app that ask the user repeatedly to input a word until the time expires. I have manage to use this for display the counter

`
            for (int a = 20; a >= 0; a--)
            {
                Console.Write("\rTIME COUNTDOWN {0:00}", a);
                System.Threading.Thread.Sleep(1000);
            }` 

The console will ask the user to input a word, I want to read the input and then repeat the question and read again input until the time expires.

        Console.WriteLine($"insert word");
     
        var input = Console.ReadLine();

How can I achieve this? Any help?

  • 2
    Need more details: if the time expires, do you need the UI to interrupt the user, or can it wait until they've entered the word one more time and then exit? The former will be [much more complicated](https://stackoverflow.com/q/57615/120955) than the latter. – StriplingWarrior Jan 27 '22 at 21:01
  • need to display a message that game is interrupted and show points – Nightwher Nightwher Jan 27 '22 at 22:04

1 Answers1

0

you can use WaitHandle.WaitOne method

the countdown loop will run and the user will enter an input and when the countdown is finished, the score will be printed and the thread will be finished and there will be a signal to print the user score.

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

namespace ConsoleApp2
{
    class Program
    {
        static AutoResetEvent autoEvent = new AutoResetEvent(false);
        static bool flg = true;
        static int count = 20;
        static void Main()
        {
            Console.WriteLine($"insert word");
            for (int a = 20; a >= 0; a--)
            {
                //Console.WriteLine("\rTIME COUNTDOWN {0:00}", a);
                System.Threading.Thread.Sleep(1000);
                count = a;
                ThreadPool.QueueUserWorkItem(
                    new WaitCallback(GetInputFromUser), autoEvent);

            }



            // Wait for GetInputFromUser method to signal.
            autoEvent.WaitOne();
            Console.WriteLine("you scored x points");

        }

       

        static void GetInputFromUser(object stateInfo)
        {

            if (Console.KeyAvailable || flg)
            {
                flg = false;
                var input = Console.ReadLine();
                Console.WriteLine($"insert word");
                
            }
            ((AutoResetEvent)stateInfo).Set();

        }


    }
}
LirHaz
  • 36
  • 3
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 29 '22 at 03:58
  • This appears to wait until one key is pressed, then the user has unlimited time to input then text. – Enigmativity Jan 29 '22 at 06:27
  • i cahnged so if there is no key enters from the user, ((AutoResetEvent)stateInfo).Set() will execute and signal to print the user score – LirHaz Jan 29 '22 at 08:22