2013-10-03 24 views
0

我一直在玩Kinect for Windows SDK 1.8一段時間,只是在一段時間後重新熟悉它自己。我有一個基本的應用程序運行,它使用顏色和骨架流來覆蓋用戶的視頻輸入框上的骨架,同時還實時顯示他們軀幹的X,Y和Z座標。所有這些都很完美,但我遇到了關閉應用程序的問題。第一,我Window_Close事件是這樣的:如何在窗口關閉時完全停止所有Kinect流和函數?

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(); 
} 

我加入了「Application.Current.Shutdown()」只是因爲我的程序將掛起,並沒有真正接近時,我關上窗戶。我通過函數發現它掛在this.mainKinect.Stop()上,其中mainKinect是指示物理Kinect的Kinect對象。我想也許它不能正確關閉兩個流,所以我在Stop()之前加了

this.mainKinect.ColorStream.Disable(); 
this.mainKinect.SkeletonStream.Disable(); 

。我發現它實際掛在SkeletonStream.Disable()上,我不知道爲什麼。我的代碼中大部分代碼都是直接來自他們的示例,所以我不知道爲什麼這不起作用。如果您有任何想法,或希望我發佈更多我的代碼,請不要猶豫。

回答

2

我總是檢查所有的流,如果它們被啓用。任何啓用的流禁用,下一步將分離所有先前附加的事件處理程序,最後我在try-catch塊中調用Stop()並記錄異常消息以在出現任何問題時獲得提示。

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) 
    } 
} 

希望這會有所幫助。

+0

謝謝,週一我回去工作時,我會試一試。唯一的問題是,它似乎沒有在Stop()上拋出異常,它只是坐在那裏。 –

+1

我重新發現sdk的某些舊版本(1.5或1.6)是一個「bug」,這樣,如果您在停止之前忘了一次禁用流,那麼您必須關閉kinect才能正確斷開連接。我遇到了這個問題,在我用以上方式停止kinect之後,這個問題就消失了,但是我沒有測試新的sdk是否存在bug。 – nhok

+0

有趣的是,我會重新啓動Kinect,然後看看這個問題是否會消失。我使用SDK 1.8,但值得一試。 –

相關問題