title: How can I exit (stop) my Dispatcher?
body: <p>I would like to stop my dispatcher which has been started in the main(), when the user clicks a button. </p>
  
  <pre><code>    private void button_start_Click(object sender, RoutedEventArgs e)
      {
          ...
              Camera.EventFrame -= onFrameEventFocusCam;  //unsubscribe event
  
              while (!Dispatcher.HasShutdownStarted)
              {
                  // test if Dispatcher started shutdown  --&gt; ok, it does but never finishs...!
              }
          ...
      }        
  
  
      private void onFrameEventFocusCam(object sender, EventArgs e)
      {
         ...
  
          Dispatcher.Invoke(new Action(() =&gt;
          {
              // Convert bitmap to WPF-Image
              var bmp = new Bitmap(bitmap);
              var hBitmap = bmp.GetHbitmap();
  
              System.Windows.Media.ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
              hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
  
              image_fokus.Source = wpfBitmap;
              image_fokus.Stretch = System.Windows.Media.Stretch.UniformToFill;
  
  
              DeleteObject(hBitmap);
              bitmap.Dispose();
  
          }));
          GC.Collect();
      }
  </code></pre>
  
  <p>When I run this code I get following error message while it is stopped in the <code>onFrameEventFocusCam</code>:</p>
  
  <pre><code>  Error-Message: Thread was interrupted from a waiting state.
  </code></pre>