I am having some timing issues with the following code. Basically I have "recordingOn", when True, it's suppose to start writing frames to file. In the below program, when I sometimes do Stop_Button_Click, I get an exception on "writer1.Write(frame1);" line, this is likely because it's doing so after I've already done dispose(). How do I synchronize this? thanks!
private bool recordingOn = false;
private void ConnectCameras_Button_Click(object sender, EventArgs e)
{
if (!captureInProgress) //Start cameras streaming
{
camera1Capture.ImageGrabbed += ProcessFrame;
camera1Capture.Start();
}
else //Stop cameras streaming
{
camera1Capture.Stop();
imageBox1.Image = null;
camera1Capture.ImageGrabbed -= ProcessFrame;
}
captureInProgress = !captureInProgress;
}
private void ProcessFrame(object sender, EventArgs arg)
{
camera1Capture.Retrieve(frame1);
imageBox1.Image = frame1;
if (recordingOn)
{
try
{
writer1.Write(frame1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void Stop_Button_Click(object sender, EventArgs e)
{
// Doing other stuff...
recordingOn = false;
writer1.Dispose();
}