删除eventHandler时停止调用

时间:2016-07-25 10:06:16

标签: c# multithreading

我正在开发一个应用程序c#(我不是唯一一个开发人员而且我是线程管理的初学者)。

当我关闭应用程序时,我调用一个函数来停止相机运行(使用directshow.NET)。该函数需要所有线程都结束。我在等待模式下遇到一个功能问题,它会阻止结束。

代码:

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);
}

实际上主线程在mediaCtrl.Stop()被阻止。我已停止执行,我看到一个工作线程被阻止:

  

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)
  mscorlib.dll!System.Threading.WaitHandle.WaitOne(System.TimeSpan timeout,bool exitContext)
  WindowsBase.dll中!System.Windows.Threading.DispatcherOperation.DispatcherOperationEvent.WaitOne()
  WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Wait(System.TimeSpan timeout)
  WindowsBase.dll!System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherOperation操作,System.Threading.CancellationToken cancellationToken,System.TimeSpan timeout)
  WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority,System.TimeSpan timeout,System.Delegate方法,object args,int numArgs)
  WindowsBase.dll!System.Windows.Threading.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority priority,System.Delegate method,object arg)
  WpfCap.dll!WpfCap.CapDevice.capGrabber_NewFrameArrived(对象发送者,System.EventArgs e)   WpfCap.dll!WpfCap.CapGrabber.OnNewFrameArrived()   WpfCap.dll!WpfCap.CapGrabber.BufferCB(double sampleTime,System.IntPtr buffer,int bufferLen)

我想当我从EventHandler capGrabber_NewFrameArrived中删除方法NewFrameArrived时,它不会停止执行。如何在执行capGrabber.NewFrameArrived -= new EventHandler(capGrabber_NewFrameArrived);之前强行关闭线程?

0 个答案:

没有答案