title: C#.NET Threading Question
body: <p>I am facing an issue with communication between threads in a C#.NET application.
  Hope someone will guide me in the right direction about the possible solutions.</p>
  
  <p>I have an application in C#.NET.It is a windows form application.
  My application has two threads - One thread is the main thread (UI thread) and the other one is the child thread. Lets call the child thread the "workerThread"
  There is only one form used in the application.Lets call this form the "MainForm"</p>
  
  <p>The child thread is started when the MainForm loads (used the form's "Load" event handler to start the thread)</p>
  
  <p>In the MainForm class, I have a variable named "stopWork" which is a public boolean variable and it serves as a flag to indicate whether the child thread should continue working or should it stop</p>
  
  <p>I have another class (besides the MainForm class) which contains the method that I execute in the the child thread. Lets call this second class the "WorkerClass".
  I pass a reference to the current form (the MainForm) into the constructor of the "WorkerClass" </p>
  
  <p>I have a button "stop" in the main form which sets "stopWork" to "true" if its clicked and then calls "workerThread.Join()" to wait for the child thread to finish excecution.</p>
  
  <p>In the child thread, the method "doWork" keeps checking the status of "parentForm.stopWork" inside a <em>for</em> loop. If "stopWork" is set to "true" then the loop breaks and subsequently the method ends.</p>
  
  <p>Now, the issue is, once I am clicking the "stop" button ,the application hangs.</p>
  
  <p>I am pasting parts of the code below so that it is easier to understand :</p>
  
  <pre><code>public partial class MainForm : Form
  {
      Thread workerThread = null;
      ThreadStart workerThreadStart = null;
      WorkerClass workerClass = null;
  
      public bool stopWork = true;
  
      /*.......... some code ............*/
  
      private void MainForm_Load(object sender, EventArgs e)
      {
          workerThreadStart = new ThreadStart(startWork);
          workerThread = new Thread(workerThreadStart);
          stopWork = false;
          workerThread.Start();
      }
  
      private void startWork()
      {
          workerClass = new WorkerClass(this);
      }
  
      private void buttonStop_Click(object sender, EventArgs e)   //"stop" button
      {
          if (workerThread != null)
          {
              if (workerThread.IsAlive == true)
              {
                  stopWork = true;
                  workerThread.Join();
              }
          }
      }
  
      /*.......... some more code ............*/
  
  }
  
  public class WorkerClass
  {
      MainForm parentForm=null;
  
      /*......... some variables and code ........*/
  
      public WorkerClass(MainForm parentForm)
      {
          this.parentForm=parentForm;
      }
  
      /* .............. some more code ...........*/
  
      public void doWork()
      {
         /*.......... some variables and code ...........*/
  
         for(int i=0;i&lt;100000;i++)
         {
             // ** Here is the check to see if parentForm has set stopWork to true **
             if(parentForm.stopWork==true)
                break;
  
             /*......... do some work in the loop ..........*/
  
  
         }
  
      }
  
      /********* and more code .........*/
  }
  </code></pre>
  
  <p>I think I may know where the problem lies.
  The problem is in the "doWork" method in the child thread trying to access "stopWork" variable in the parent form when already the parent form is blocked by calling the "workerThread.Join()" method. So ,I think this is a "deadlock" problem.</p>
  
  <p>Am I right in identifying the problem ?  Or am I wrong and the problem lies somewhere else ?</p>
  
  <p>In case this is indeed a deadlock, what are the possible solutions to solve this ?</p>
  
  <p>I did a bit of googling and found lots of resources on thread synchronisation and how to avoid deadlocks. But I could not understand how to apply them specifically to my problem.</p>
  
  <p>I would really appreciate any help or guidance on resolving this issue.</p>