title: How to stop event-handler?
body: <p>To show the live-stream of a camera an event-handler is started. The code for this event is:</p>
  
  <pre><code>       private void onFrameEventFocusCam(object sender, EventArgs e)
      {
          uEye.Camera Camera = sender as uEye.Camera;
  
          Int32 s32MemID;
          Camera.Memory.GetActive(out s32MemID);
  
  
          // Read Camera bitmap
          Bitmap bitmap;
          Camera.Memory.ToBitmap(s32MemID, out bitmap);
  
  
          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.Fill;
              image_fokus.StretchDirection = StretchDirection.Both;
  
              DeleteObject(hBitmap);
              bitmap.Dispose();
  
          }));
          GC.Collect();
      }
  </code></pre>
  
  <p>Now, when I close the camera (see code below), it gives me an error-message "Thread was interrupted from a waiting state." back and the debugger jumps to the Dispatcher.Invoke-Code (see above). </p>
  
  <p>When I uncomment the MessageBox.Show("0"), the error is <strong>NOT</strong> shown anymore and everything works fine after pressing the OK-Button of this messagebox. </p>
  
  <p>This is the code for closing the camera and the event-handler:</p>
  
  <pre><code>             // Close Camera
                  Camera.Acquisition.Stop(uEye.Defines.DeviceParameter.Force);                 
                  Camera.EventFrame -= onFrameEventFocusCam;              // remove Event
  
               // MessageBox.Show("0");
  
                  uEye.Defines.Status statusRet = 0;
                  statusRet = Camera.Exit();
  </code></pre>
  
  <p>Does someone knows what I am doing wrong and how to avoid such error-message (without the messagebox)?</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <h2>UPDATED Code</h2>
  
  <p>These steps are performed:</p>
  
  <p>1) Camera is opened and the <strong>onFrameEventFocusCam</strong> event-handler is called</p>
  
  <p>2) The image is valid and shown on the GUI</p>
  
  <p>3) In the Main UI the camera is closed again and the event disabled</p>
  
  <p>4) For some reasons (race-condition?) the same event is re-activated again</p>
  
  <p>This is the part of the main-thread where the camera is closed again:</p>
  
  <pre><code>    else
      {
        // Close Camera     
        uEye.Defines.Status statusRet = 0;
        statusRet = Camera.Acquisition.Stop(uEye.Defines.DeviceParameter.DontWait);
  
        Camera.EventFrame -= onFrameEventFocusCam;              // remove Event
        statusRet = Camera.Exit();
  
      } // end of else.. 
  </code></pre>
  
  <p>After the "}" the following event is reactivated again and it jumps again to the onFrameEventFocusCam!</p>
  
  <pre><code>    private void onFrameEventFocusCam(object sender, EventArgs e)
      {
          uEye.Camera Camera = sender as uEye.Camera;
  
          Int32 s32MemID;
          Camera.Memory.GetActive(out s32MemID);
  
  
          // Read Camera bitmap
          Bitmap bitmap;
          Camera.Memory.ToBitmap(s32MemID, out bitmap);
  
  
          Dispatcher.BeginInvoke(DispatcherPriority.Normal, 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.Fill;
              image_fokus.StretchDirection = StretchDirection.Both;
  
              DeleteObject(hBitmap);
              bitmap.Dispose();
  
          }));
          GC.Collect();
      }
  </code></pre>