I've been playing around with Kinect for Windows SDK 1.8 for a bit, just re-familiarizing myself with it after some time away. I have a basic application running that uses the color and skeleton streams to overlay a skeleton on a video feed of the user, while also displaying their torso's X, Y, and Z coordinates in realtime. All of this works perfectly, but I've run into an issue with shutting the application down. A first, my Window_Close event looked like this:
private void Window_Closed(object sender, EventArgs e)
{
// Turn off timers.
RefreshTimer.IsEnabled = false;
RefreshTimer.Stop();
UpdateTimer.IsEnabled = false;
UpdateTimer.Stop();
// Turn off Kinect
if (this.mainKinect != null)
{
try
{
this.mainKinect.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.TxtBx_KinectStatus.Text += "\n[" + DateTime.Now.TimeOfDay.ToString() + "] " + this.mainKinect.UniqueKinectId.ToString() + " has been turned off.";
}
// Shut down application
Application.Current.Shutdown();
}
I added the 'Application.Current.Shutdown()' only because my program would hang and never actually close when I closed the window. I stepped through the function to find that it hangs on this.mainKinect.Stop(), where mainKinect is the Kinect object referring to the physical Kinect. I thought that maybe it couldn't shut down both streams properly, so I added
this.mainKinect.ColorStream.Disable();
this.mainKinect.SkeletonStream.Disable();
just before the Stop(). I found out that it actually hangs on the SkeletonStream.Disable(), and I don't know why. Most of the rest of my code is straight from their examples, so I don't know why this doesn't work. If you have any ideas, or would like me to post more of my code, please don't hesitate.
I always check all streams, if they are enabled. Any enabled stream I disable, the next step to detach all previously attached eventhandlers, at the end I call the Stop() in a try-catch block and logs the Exception Message to get a hint in the case of any problem.
public void StopKinect()
{
if (this.sensor == null)
{
return;
}
if (this.sensor.SkeletonStream.IsEnabled)
{
this.sensor.SkeletonStream.Disable();
}
if (this.sensor.ColorStream.IsEnabled)
{
this.sensor.ColorStream.Disable();
}
if (this.sensor.DepthStream.IsEnabled)
{
this.sensor.DepthStream.Disable();
}
// detach event handlers
this.sensor.SkeletonFrameReady -= this.SensorSkeletonFrameReady;
try
{
this.sensor.Stop()
}
catch (Exception e)
{
Debug.WriteLine("unknown Exception {0}", e.Message)
}
}
hope this helps.