title: C# - Display Countdown Timer in Console for User Input Through ReadLine()
body: <p>Currently I am trying to create a console game. The basic concept is that the user gets a number of randomized letters and has a limited amount of time to make as many words with these letters as possible. An example could be, that the user gets [a,a,c,t,t,e,g,s,o,i] where valid words would be "get", "got", "test", etc. The user input is checked on whether it is present in a word list and whether it consists of letters that are allowed to be used. Unfortunately I have some trouble trying to implement and display the timer for this game.</p>
  
  <p>Please note: My C# knowledge is very limited, as I am just a beginner.</p>
  
  <p><strong>The problem</strong></p>
  
  <p>At the moment I have a background thread that contains a loop with the ReadLine() function. The 'normal' code pauses with the Sleep() function and continues when the time is up. It is heavily inspired by the solution given <a href="https://stackoverflow.com/questions/57615/how-to-add-a-timeout-to-console-readline">here</a>. What I am hoping to achieve is to display a timer in the console, that tells the user how many seconds he has left to fill in words. However, I have not figured out how to achieve this. </p>
  
  <p>I have trouble thinking up a solution, because of the following. The reason the timer words is because after the 'normal' code is paused, the Thread containing the loop asking for userinput, is active without interruptions. This means that there are no interruptions that could allow a timer to printed. I have no idea on how to periodically 'pause' the readline function while maintaining its functionality.</p>
  
  <p>So my question to you is, how could I achieve this?</p>
  
  <p><strong>The code</strong></p>
  
  <p>This is a piece of isolated code containing just this functionality. So the words are not tested on whether they meet the requirements.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Threading;
  
  namespace UnderstandingThreading
  {
  
      class Reader
      {
          private static Thread inputThread;
          private static List&lt;string&gt; userInput = new List&lt;string&gt;();
          private static bool closeLoop = new bool();
  
          static Reader()
          {
              inputThread = new Thread(reader);
              closeLoop = false;
              inputThread.IsBackground = true;
              inputThread.Start();
          }
  
          private static void reader()
          {
              while (!closeLoop)
              {
                  userInput.Add(Console.ReadLine());
              }
          }
  
          public static List&lt;string&gt; ReadLine(int timeOutMilliseconds)
          {
              Thread.Sleep(timeOutMilliseconds);
              closeLoop = true;
              return userInput;
          }
      }
  
  
      class MainClass
      {
          public static void Main(string[] args)
          {
              List&lt;string&gt; allEntries = new List&lt;string&gt;();
              Console.WriteLine("Please enter random things for the next 5 seconds");
              allEntries = Reader.ReadLine(5000);
  
              for (int i = 0; i &lt; allEntries.Count; i++)
              {
                  Console.WriteLine(allEntries[i]);
              }
  
          }
      }
  }
  </code></pre>
  
  <p>Thank you,</p>
  
  <p>Sebastiaan</p>