I am just learning how to work with threading mechanism in C#.
I took a example from msdn and altered it to design a simple game like
this.
But the problem is the while loop in DoWork method is reading keys even
before DoWork Method is called from main program. I,e. When you run the
program before "Go:" appears on the screen if you type some thing while
loop in DoWork method is reading keys. But control should be passed to
while loop after printing "Go:" on screen, Right. Can someone kindly
explain me why it is happening like this. Thank You.public class Worker
{
ConsoleKeyInfo cki;
StringBuilder sb = new StringBuilder();
bool f = true;
// This method will be called when the thread is started.
public void DoWork()
{
Console.SetCursorPosition(49, 8);
Console.Write("Go:");
Console.SetCursorPosition(53, 8);
while (!_shouldStop)
{
cki = Console.ReadKey(true);
if (f == true && (65 <= Convert.ToInt32(cki.Key) && Convert.ToInt32(cki.Key) <= 90))
{
Console.Write(cki.Key.ToString());
sb.Append(cki.Key.ToString());
}
}
while (true)
{
if (cki.Key.ToString() == "Enter") break;
cki = Console.ReadKey(true);
if (cki.Key.ToString() == "Enter") break;
}
}
public void RequestStop(string word)
{
_shouldStop = true;
f =false;
Console.WriteLine();
Console.SetCursorPosition(44, 10);
Console.WriteLine("- TIME OUT -");
Console.SetCursorPosition(46, 12);
if (sb.ToString() == word.ToUpper())
{
Console.WriteLine("CORRECT !");
Console.SetCursorPosition(42, 13);
Console.WriteLine("CONGRATULATIONS");
}
else { Console.SetCursorPosition(47, 12); Console.WriteLine("WRONG !"); }
Console.SetCursorPosition(40, 15);
Console.WriteLine("Press [Enter] to quit");
Console.CursorVisible = false;
}
private volatile bool _shouldStop;
}
public class WordPlay
{
static void Main()
{
Console.SetBufferSize(100,50);
Console.SetWindowSize(100,50);
string[] words = { "angstrom", "abacinate", "abactinal", "abandoned", "Babesiidae", "babirussa", "Babylonian", "bacchantic", "cabassous", "cableway" };
string word="";
Random randObj = new Random(0);
Console.SetCursorPosition(40, 6);
Console.WriteLine("Your String:");
Console.WriteLine();
for (int j = 0; j < 20; j++)
{
System.Threading.Thread.Sleep(150); Console.SetCursorPosition(53, 6);
Console.Write(words[randObj.Next(words.Length - 1)].ToUpper() + " ");
}
word = words[randObj.Next(words.Length - 1)].ToUpper();
Console.SetCursorPosition(53, 6);
Console.Write( word+" ");
Console.WriteLine();
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
workerThread.Start();
// Loop until worker thread activates.
while (!workerThread.IsAlive);
// Put the main thread to sleep for 1 millisecond to
// allow the worker thread to do some work:
Thread.Sleep(3000);
// Request that the worker thread stop itself:
workerObject.RequestStop(word);
// Use the Join method to block the current thread
// until the object's thread terminates.
workerThread.Join();
}
Loading
Sam HobbsPosted Mar 4, 2010, 1:13 AM
I assume you are using a console program because console programs appear easier. An interactive console program might be more work than a simple GUI form application. If you are using a console program because it appears easier, then it might be better to spend time learning GUI applications.
I don't see the use of while (!workerThread.IsAlive). I think that can be removed.
I understand that you are learning about threads, but this particular example might be confusing. The application does not need another thread; you just call Sleep in your main thread while the worker thread is working.
Something you can do that will help you become familiar with threads is to use a timer and wait on the timer instead of using Sleep. If you use a timer, then you should use WaitHandle.WaitAny to wait on both the timer and the thread, in case the thread finishes before the timer does. When you have that figured out, you will have learned some useful stuff.
Weimin YangPosted Mar 3, 2010, 5:11 PM
The .Net Frameworks's System.Console class does not support screen buffers. Jim Mischel's articles http://www.devsource.com/c/a/Using-VS/Working-with-Console-Screen-Buffers-in-NET/ may give answer to your question.