I am trying to mock up of a chat client. First here's the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace thread
{
class Program
{
public static Thread t1;
public static Thread t2;
public static bool flag;
public static Random rand = new Random();
static void Main(string[] args)
{
t1 = new Thread(first);
t2 = new Thread(second);
t1.Start();
t2.Start();
Console.Read();
}
public static void first()
{
string[] phrase = { "Hello", "random", "blah,blah", "computer", "Welcome", "This is chat bot" };
while (!flag)
{
Thread.Sleep(4000);
Console.WriteLine("{0}", phrase[rand.Next(6)]);
}
}
public static void second()
{
string input = "";
while (input!="x")
{
input=Console.ReadLine();
if (input=="x")
{
break;
}
}
flag = true;
}
}
}
Ok so this program would automatically print some text on console, and i can write my message on the screen too. Now the problem is that whenever i am typing a long sentence, anything that takes more than 4 seconds to type. Then instead of the automated message being printed on the next line it just gets append to whatever i am typing. I am really new to multi threading so i am not exactly sure what's the problem. I think both the threads are using the same console class.
Help would be appreciated in this regard.