I wanted to pause a VideoStream from a Kinect. The button has the following code:
if (capturing==0)
{
try
{
// Signing the Event for Image frame ready
nuiRuntime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_VideoFrameReady);
nuiRuntime.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_DepthFrameReady);
nuiRuntime.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
nuiRuntime.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.Depth);
}
catch
{
Dialogs.ShowWarning("An Error occured. Check, if the Kinect is connected properly.");
}
finally
{
capturing = 2;
}
}
else if (capturing == 1)
{
nuiRuntime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_VideoFrameReady);
nuiRuntime.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_DepthFrameReady);
capturing = 2;//go on after break
}
else
{
nuiRuntime.VideoFrameReady -= new EventHandler<ImageFrameReadyEventArgs>(nuiRuntime_VideoFrameReady);
nuiRuntime.DepthFrameReady -= nuiRuntime_DepthFrameReady;
capturing = 1;//pause
}
I dont think, it is hard to understand.
States: 0, Kinect is uninitialized, do so
1, Kinect is paused, go on
2, Kinect is normally capturing
But: when the “else” is executed, the Image in the GUI (WPF.Image) still shows a moving video-stream. In the “else” are 2 ways, I tried to make it a break. Then there should be displayed a still picture.
Of course, I simply can say the EventHandler: If we are in state 1, simply don’t work on the picture, but I don´t think, this is a clean way of implementation.
Can anyone see my mistake?
Event handlers are reference types (delegates), the one you add to the event
is not the same one you remove from it:
The fix is simple, keep the handler instance somewhere:
then add/remove the same instance:
BTW, the code you use in the “else” block:
is the same as:
The compiler simple do the
new EventHandler<>things for you.