1

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

| improve this question | | | | |
  • 5
    C# console applications are not very suitable for this kind of UI. Have you considered writing this as a GUI application, using something like Winforms or WPF? – svick Aug 17 '14 at 18:02
  • 2
    Some info about console output here: stackoverflow.com/questions/199321/advanced-console-io-in-net – Den Aug 18 '14 at 8:08
  • For something like printing a timer in console without disrupting your existing math problem, you'll need some support from the terminal. There are libraries such as curses to aid you with this, but it's a C library and I don't know if such a library is well supported in the Windows/.NET ecosystem. – Rufflewind Aug 19 '14 at 14:11
3

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.

| improve this answer | | | | |
2

I don't think console application is suitable for this kind of challenged UI controls.

I advice using much smarter objects. Anyway for making the timer you can use a Time and on the Tick event you can call the function to stop the game and show score.

And for time you can get DateTime.Now but this is not that dynamic result it will be just the time of starting.

If you really want to use a console you can make two console apps the first one is the game and the second is timer that sends window messages to the first when tick event pops out.

| improve this answer | | | | |
  • yes, he should not use a console. Trying to create a console window and then positioning elements in it is a recipe for disaster. These aren't the 1980s when we had console libraries for that in lue of GUIs after all. – jwenting Aug 19 '14 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.