title: Pausing background thread with AutoResetEvent.WaitOne() and .Set()
body: <p>I have following code. I'm trying to make buttons to main Form which can Pause, Continue and Stop the background thread the downloader is running on (private Thread thread)</p>
  
  <p>Form1.cs</p>
  
  <pre><code>private AutoResetEvent waitHandle = new AutoResetEvent(true);
  private Thread thread;
  
          private void ThreadJob()
          {
              Downloader download = new Downloader();
              download.runDownloader();
          }
  
          // THREADS button1 is "Download now"-button
          private void button1_Click(object sender, EventArgs e)
          {
              ThreadStart job = new ThreadStart(ThreadJob);
              thread = new Thread(job);
              thread.IsBackground = true;
              thread.Start();
          }
  </code></pre>
  
  <p>This code is ran on Windows Form. I have buttons for all those actions (Pause, Continue, Stop)</p>
  
  <p>Pause and continue button has code on form</p>
  
  <pre><code>private void btnPause_Click(object sender, EventArgs e)
  {
      waitHandle.WaitOne(); // Need to pause the background thread
  }
  
   private void btnContinue_Click(object sender, EventArgs e)
      {
          waitHandle.Set(); // Need to continue the background thread
      }
  </code></pre>
  
  <p>The problem is pressing Pause button will freeze the main Form not the background thread. </p>