C# – How to set a time limit for a game

cmultithreadingvisual studio

I am learning the multi-threading and timer in C# now. But it seems I can't find a good solution.

For example, I would like to see how many addition problems that I can solve within 1 min. I would like my program to have

  1. A digital clock to count for 60 seconds in the top of my Console.
  2. Print a math problem in the middle of my console wait for my input.
  3. When 60 seconds is done, stop the math problem challenges immediately (most of time, it is still waiting for my input, but we will stop it immediately).
  4. Count how many correct problems that I have solved.

Two challenges of the program now.
a) how can we make sure the print time and math problem do not mess up.
b) how can we stop the math challenges part immediately after time is up

Best Answer

I would suggest you rather use a Win Form app for game. If you use Console you will need to refresh (clear and redraw) the console each time you want to change display - that is with change in each second.

On a win form you can directly put a timer control and start the timer on game start (button click, form load - as per your need).

When timer tick event fires you can just disable the current question and show the score.