0

I'm a beginner in C#. I'm developing a console game and I have a problem with Thread in C#.

My game will display a top bar where count down timer runs. I try with a thread, I use Console.Clear() to clear old number then replace by new number on one line (59,58,57...). My game display a message to user input user's data on center screen or anywhere,...etc. But, when I start the thread countdown, it clear console screen, also, it clear message that user can input user's data. Can you help me and explain how to start 2 threads, do more various tasks?

using System; using System.Threading;
namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
        Program m = new Program();
        Thread pCountDown = new Thread(new ThreadStart(
            m.DisplayCountDown   
        ));
        Thread pDisplayForm = new Thread(new ThreadStart(
            m.DisplayForm    
        ));
        pCountDown.Start();
        pDisplayForm.Start();
        Console.ReadKey();
    }

    private void DisplayCountDown() {
        for (int i = 60; i >= 0; --i) {
            Console.Write("Time: {0}",i);
            Thread.Sleep(1000);
            Console.Clear();
        }

    }

    private void DisplayForm() {
        while (true) {
            Console.Write("Enter your number: ");
            int a = Int32.Parse(Console.ReadLine());
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }
 }
}

Error: My error

I want like this:

Image (Sorry, i'm a new member): Like this

Do Manh
  • 144
  • 2
  • 9
  • I'm certainly no expert in console displays, so hopefully someone has a better suggestion. But it would seem at the very least that you need to re-draw the whole screen on each countdown step, not just the timer. And even then you're going to run into the problem of clearing the user's input every second (or at least the visibility of their input, which could lead to an odd UX). I'm not sure if there's a way to section off parts of the console for clearing... – David Jul 04 '12 at 08:13

3 Answers3

1

You don't need thread nor to clear the console. Simply use Console.SetCursorPosition() and Console.Write() as suggested here, so you can overwrite the number.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

You do not need to clear the console. Console.Write() writes over the existing characters, so just change the cursor position with Console.SetCursorPosition(x,y);

For example:

string mystring = "put what you want to right here";
Console.SetCursorPosition(0,0); //the position starts at 0 just make a note of it
Conolse.Write(mystring);

//now when you are ready to clear the text and print something over it again
//just add this

//now first erase the previous text
for(int i = 0; i< mystring.Length; i++)
{
    Console.SetCursorPosition(i,0);
    Console.Write(' ');
}

//now write your new text
mystring = "something else";
Console.SetCursorPosition(0,0);
Console.Write("mystring");
erik.dreyer
  • 17
  • 1
  • 8
0

Here's a sample DisplayCountDown which doesn't clear the whole screen every second:

private void DisplayCountDown()
{
    for (int i = 20; i >= 0; --i)
    {
        int l = Console.CursorLeft;
        int t = Console.CursorTop;
        Console.CursorLeft = 0;
        Console.CursorTop = 0;
        Console.Write("Time: {0}    ", i);
        Console.CursorLeft = l;
        Console.CursorTop = t;
        Thread.Sleep(1000);
    }
}

However, this still leaves some issues. In my case I saw "Enter your number" appearing on the top line and being overwritten, so had to add the line

if (Console.CursorTop == 0) Console.CursorTop = 1;

inside the while loop. Also, if the user enters enough numbers, the countdown scrolls out of view, and if you try to scroll up to look at it, setting the cursor position automatically scrolls back.

I also had intermittent issues with int.Parse throwing an exception, presumably caused by the countdown changing at some critical point of the user's input.

Rawling
  • 49,248
  • 7
  • 89
  • 127