2
Reply

How to work with threads effectively?

Nagarjuna Naidu

Nagarjuna Naidu

14 years ago
3.2k
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();      
}

Answers (2)