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