title: Thread synchronization in WinCE
body: <p>I have faced with a problem of threads synchronization. My presenter analyzes some sensors and update UI form. I moved updating code into separate thread. It works fine, but if the user stops presenter when it is updating the view, the software freezes - I found that it happens when view.UpdateUI working (it just set some labels using Invoke). Where my problem is? I use compact framework 3.5 and Windows CE 5</p>
  
  <pre><code>using System.Threading;
  
  class MyPresenter
  {
    UserControl view;
  
    private Thread thread;
    private ManualResetEvent cancelEvent;
  
    public void Start()
    {
      cancelEvent = new ManualResetEvent(false);
      thread = new Thread(UpdateView) { IsBackground = true };
      thread.Start();
    }
  
    public void Stop()
    {
      if (thread != null) {
        cancelEvent.Set();
        thread.Join();
        thread = null;
      }
    }
  
    private void UpdateView()
    {
      while (cancelEvent.WaitOne(1000, false) == false) {
        // analyze something
        view.UpdateUI(...);
      }
    }
  }
  </code></pre>