这是子窗口关闭时,调用的代码
private void VideoWnd_FormClosing(object sender, FormClosingEventArgs e)
{
capture.Pause();
capture.Stop();
for(int i=0;i<999999999;i++)
{
if (capture._grabState == Emgu.CV.Capture.GrabState.Stopped)
capture.Dispose();
}
}
修改Capture类的结构体,查看_grabState字段始终未stopping状态
下面为Capture类中线程运行代码
private void Run()
{
try
{
while (_grabState == GrabState.Running || _grabState == GrabState.Pause)
{
if (_grabState == GrabState.Pause)
{
_pauseEvent.WaitOne();
}
else if (IntPtr.Zero.Equals(_ptr) || !Grab())
{
//capture has been released, or
//no more frames to grab, this is the end of the stream.
//We should stop.
_grabState = GrabState.Stopping;
}
}
}
catch (Exception e)
{
throw new Exception("Capture error", e);
}
finally
{
_grabState = GrabState.Stopped;
}
}
在子窗口中添加按钮,关闭摄像头能够正常工作。
在摄像头打开的情况下,直接点击关闭按钮,无论怎样都会产生catch错误,
错误为:引用已经释放的对象,ImageBox
1.理论上不是我在VideoWnd_FormClosing事件中调用了capture.stop()方法,线程不就应该调用finally中的代码么,但是直到for循环结束,线程都没有调用。不知道为什么