{"id":2643,"date":"2022-08-30T15:26:34","date_gmt":"2022-08-30T15:26:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/stopping-kinect-when-pressing-stop-button-in-visual-studio-ui-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:34","modified_gmt":"2022-08-30T15:26:34","slug":"stopping-kinect-when-pressing-stop-button-in-visual-studio-ui-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/stopping-kinect-when-pressing-stop-button-in-visual-studio-ui-collection-of-common-programming-errors\/","title":{"rendered":"Stopping Kinect when pressing Stop Button in Visual Studio UI?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m using Kinect in a WPF app and Dan Fern\u00e1ndez taught us in his quickstart video series how to stop the sensor when calling the event &#8220;Window_Closing&#8221; (which being new into C#, I guess it&#8217;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&#8217;t stop and then I have to run the app again and close it clicking on the X button.<\/p>\n<p>Is there a more general way to stop the Kinect with some event for the app Shutdown inside my code?<\/p>\n<p>Thank you.<\/p>\n<pre><code>    void StopKinect(KinectSensor sensor) {\n        if (sensor != null) {\n            sensor.Stop();\n\n            if (sensor.AudioSource != null)\n            {\n                sensor.AudioSource.Stop();\n            }\n        }\n    }\n\n    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)\n    {\n        StopKinect(kinectSensorChooser1.Kinect); \n    }\n<\/code><\/pre>\n<ol>\n<li>\n<p>When you click the Stop button on VS you interrupt the process. So the Windows_Closing event is never called. And the Kinect don&#8217;t stop.<\/p>\n<\/li>\n<li>\n<p>How are you defining whether the Kinect sensor has stopped?<\/p>\n<p>I reccomend adding exception handling around the method you are using to stop Kinect.<\/p>\n<pre><code>try\n{\n   StopKinect(kinectSensorChooser1.Kinect); \n}\ncatch(Exception ex)\n{\n   \/\/Log Exception\n}\n<\/code><\/pre>\n<p>If the application was throwing an unhandled exception when attempting to stop the Kinect sensor this will give you a method of identifying it.<\/p>\n<p>Can you confirm whether the <code>Window_Closing<\/code> event fires? Add a breakpoint within the function and if this is not hit then you&#8217;ll know the problem. An alternative may be to clean up the Kinect Sensor with a different event as @Bob suggested. It&#8217;s possible that when VS stops debugging the process is killed and so the event you&#8217;ve included the cleanup code for is not invoked.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-04 12:03:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m using Kinect in a WPF app and Dan Fern\u00e1ndez taught us in his quickstart video series how to stop the sensor when calling the event &#8220;Window_Closing&#8221; (which being new into C#, I guess it&#8217;s a delegate, or event handler). The thing is that if I press the Stop button in the Visual Studio UI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2643","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2643"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2643\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}