C# event problems
I have some problems with my application. Heres the thing I have web cam that feeds the pictures into function wich calculate if there were any movements and where. But the problem is (my opinion) that event trigered by the new frames from camera are so often that the开发者_StackOverflow社区 code doesnt have time to finish. So where could i put some semaphores or something if this is my code: (i tried many things nothing succsessful) :
private void button1_Click(object sender, EventArgs e)
{
FinalVideoSource = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
FinalVideoSource.NewFrame += new NewFrameEventHandler(FinalVideoSource_NewFrame);
FinalVideoSource.Start();
ImageEfects.drawGrid(detectionImage, 40);
}
public void FinalVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
image = (Bitmap)eventArgs.Frame.Clone();
#region demoInit
if (initalization)
{
image1 = image;
initalization = false;
}
#endregion demoInit
ImageProcessing.calculateDiferences(ref image, ref image1, ref detectionImage);
screen1.Image = image;
screen2.Image = detectionImage;
}
Just don't compare each and every frame. I would store a frame, than wait (count) a specified number of frames and than compare the current frame and the stored frame (in a separate thread). And store the current frame for the next comparison.
The number of frames to wait is up to you, that depends on which time lap is acceptable for you to overlook a movement.
I would use a flag set at the beginning of FinalVideoSource_NewFrame
which if set will let the function return. Toggle this flag at the beginning and end of your FinalVideoSource_NewFrame
. That way you will never incurr in a queue if ever a computation cycle takes longer.
精彩评论