title: C# Start and Stop Thread inside SAME button click event
body: <p>I am using C#,.Net4.0 and Visual Studio 2010 and I am trying to get this behaviour from my windows form application when the user click on a button:</p>
  
  <ol>
  <li>A GUI LED starts to blink</li>
  <li>A long rung operation starts</li>
  <li><p>When the operation at point 2 ends the LED stops blinking</p>
  
  <pre><code>private void Btn_WebService_Click(object sender, EventArgs e)
  {
      Thread_Blink = new Thread(() =&gt; { LED_Blink(LED.WS); });
      Thread_Blink.Start();
  
      // Do something that takes time.. Let's imulate with a sleep
      Thread.Sleep(2000);
  
      Thread_Blink.Abort();
  }
  </code></pre></li>
  </ol>
  
  <p>I also tried using 3 different events and/or timers..</p>
  
  <pre><code>private void Btn_WebService_MouseDown(object sender, MouseEventArgs e)
  {
     Timer_Blink.Enabled = true;
  }
  
  private void Btn_WebService_Click(object sender, EventArgs e)
  {
      // Do something that takes time.. Let's imulate with a sleep
      Thread.Sleep(2000);
  }
  
  private void Btn_WebService_MouseUp(object sender, MouseEventArgs e)
  {
      Timer_Blink.Enabled = false;
  }
  </code></pre>
  
  <p>The result is always the same: The LED starts to blink only AT THE END of the long running operation (<code>Thread.Sleep(2000);</code>) and suddenly STOPS so that you can't see anything. Why does this happen and how can I get the desired behaviour?</p>
  
  <p>I add further infos. I tried to use BackgroundWorked and implemented the wanted behavour in this way:</p>
  
  <pre><code>private void Btn_WebService_Click(object sender, EventArgs e)
      {
          BlinkWorker.RunWorkerAsync(LED.WS);
          LedOn(LED.WS);
  
          TestWebService();   // This method takes about 2 seconds to answer..
  
          LedOff(LED.WS);
          BlinkWorker.CancelAsync();
      }
  </code></pre>
  
  <p>While the TestWebService() is running I get the LED off(LedOn(LED.WS);). When the TestWebService()has finished the LED comes on. 
  The BlinkWorker still does not work if started and cancelled inside the Click event.</p>
  
  <p>FYI: If I make a button that starts the blinking and another button that stops it, it works perfectly.</p>