Stopping Invoke when remove eventHandler
<p>I'm working on an application c# (I'm not the only one developer and I'm a beginner on thread management).</p>

<p>When I close the application, I call a function to stop the camera running (using directshow.NET). This function needs that all thread are ended. I've a trouble with a function in wait mode, it prevent for the end.</p>

<p>The code :</p>

<pre><code>void capGrabber_NewFrameArrived(object sender, EventArgs e)
{
    if (this.Dispatcher != null)
    {
        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, (SendOrPostCallback)delegate
        {
            if (BitmapSource != null)
            {
                BitmapSource.Invalidate();
                UpdateFramerate();
            }
        }, null);
    }
}

void Stop()
{
    capGrabber.NewFrameArrived -= new EventHandler(capGrabber_NewFrameArrived);
    IMediaControl mediaCtrl = m_FilterGraph as IMediaControl;
    mediaCtrl.Stop();
    Marshal.ReleaseComObject(m_FilterGraph);
}
</code></pre>

<p>Actually The main thread is blocked on <code>mediaCtrl.Stop()</code>. I've stop the execution and I see a working thread blocked :</p>

<blockquote>
  <p>WindowsBase.dll!System.Windows.Threading.DispatcherOperation.DispatcherOperationEvent.WaitOne
  mscorlib.dll!System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle waitableSafeHandle, long millisecondsTimeout, bool hasThreadAffinity, bool exitContext)<br>
  mscorlib.dll!System.Threading.WaitHandle.WaitOne(System.TimeSpan timeout, bool exitContext)<br>
  WindowsBase.dll!System.Windows.Threading.DispatcherOperation.DispatcherOperationEvent.WaitOne()<br>
  WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Wait(System.TimeSpan timeout)<br>
  WindowsBase.dll!System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherOperation operation, System.Threading.CancellationToken cancellationToken, System.TimeSpan timeout)<br>
  WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs)<br>
  WindowsBase.dll!System.Windows.Threading.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority priority, System.Delegate method, object arg)<br>
  WpfCap.dll!WpfCap.CapDevice.capGrabber_NewFrameArrived(object sender, System.EventArgs e) 
  WpfCap.dll!WpfCap.CapGrabber.OnNewFrameArrived()
  WpfCap.dll!WpfCap.CapGrabber.BufferCB(double sampleTime, System.IntPtr buffer, int bufferLen) </p>
</blockquote>

<p>I suppose that when I remove method <code>capGrabber_NewFrameArrived</code> from the EventHandler <code>NewFrameArrived</code> it doesn't stop execution. How can I force the thread close before doing the <code>capGrabber.NewFrameArrived -= new EventHandler(capGrabber_NewFrameArrived);</code> ?</p>