2

I'm using Kinect in a WPF app and Dan Fernández taught us in his quickstart video series how to stop the sensor when calling the event "Window_Closing" (which being new into C#, I guess it's a delegate, or event handler). The thing is that if I press the Stop button in the Visual Studio UI to stop running, my Kinect doesn't stop and then I have to run the app again and close it clicking on the X button.

Is there a more general way to stop the Kinect with some event for the app Shutdown inside my code?

Thank you.

    void StopKinect(KinectSensor sensor) {
        if (sensor != null) {
            sensor.Stop();

            if (sensor.AudioSource != null)
            {
                sensor.AudioSource.Stop();
            }
        }
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        StopKinect(kinectSensorChooser1.Kinect); 
    }
  • I have no experience in C# but probably Window_Closing is not called when the debugger exits. What happens if you override onClosing event according to this: http://stackoverflow.com/questions/5774849/how-to-override-onclose-event-on-wpf ? Does this get called in this case? – Sassa Aug 31 '12 at 18:38
  • No Bob, it make no difference. I'm also new to C# and I don't know which is the method for that event (the event of clicking on VS's stop button. Thanks – juanp_ibanez Aug 31 '12 at 19:28
  • I think [Visual Studio : executing clean up code when debugging stops](http://stackoverflow.com/questions/1033441/visual-studio-executing-clean-up-code-when-debugging-stops) and [Stop Debugging Event in C#](http://stackoverflow.com/questions/11023944/stop-debugging-event-in-c-sharp) can probably be of help – Sassa Aug 31 '12 at 20:36

2 Answers2

1

When you click the Stop button on VS you interrupt the process. So the Windows_Closing event is never called. And the Kinect don't stop.

EdgarT
  • 1,080
  • 11
  • 18
0

How are you defining whether the Kinect sensor has stopped?

I reccomend adding exception handling around the method you are using to stop Kinect.

try
{
   StopKinect(kinectSensorChooser1.Kinect); 
}
catch(Exception ex)
{
   //Log Exception
}

If the application was throwing an unhandled exception when attempting to stop the Kinect sensor this will give you a method of identifying it.

Can you confirm whether the Window_Closing event fires? Add a breakpoint within the function and if this is not hit then you'll know the problem. An alternative may be to clean up the Kinect Sensor with a different event as @Bob suggested. It's possible that when VS stops debugging the process is killed and so the event you've included the cleanup code for is not invoked.

Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102
  • Jamie, I don't understand. The kinect sensor stops when I call the StopKinect(KinectSensor) method, which is called when I click the X button of the app. I have already implemented the try catch block you suggested but I don't know how to log in WPF or to Console.WriteLine, and that block doesn't make any difference. Maybe could you be more speciffic? Anyways thanks. – juanp_ibanez Aug 31 '12 at 17:51
  • If that's the case your original post does not make sense - 'The thing is that if I press the Stop button in the Visual Studio UI to stop running, my Kinect doesn't stop'. If you are debugging the exception will be caught, allowing you to see it in VS. When you stop debugging in VS the process is killed and im not sure whether this will allow your Window_Closing event to fire in time - add a break point to the event to confirm. – Jamie Keeling Aug 31 '12 at 21:58