2012-11-09 34 views
1

美好的一天eh。「終止」一個類的實例

我正在研究一個C#項目,我們使用Microsoft Kinect,使用它來跟蹤執行一系列跳轉的人員,並生成計算膝蓋角度的骨架。

昨天晚上當我們測試主題時,我認爲可以很好地控制程序,您可以點擊並結束「當前」測試主題的輸出文件,然後啓動另一個主題。爲了跟蹤骨架,我們創建了Kinect for Windows SDK中的Tracker類跟蹤器對象。然後啓動所有跟蹤和輸出的邏輯。但是,當我們「關閉」第一個主題的輸出時,我們似乎只關閉文件,而不是跟蹤器對象的實際實例。這會導致跟蹤器保持「打開」狀態,並與我們爲新測試主體創建的跟蹤器對象的新實例一起存在,同時使用雙倍的計算資源並造成不可接受的性能下降。

如何「關閉」跟蹤器對象的第一個實例?

如果你們需要檢查代碼,請告訴我;我沒有包括它,因爲在這裏發佈是不切實際的。

編輯:

這裏至少有一些代碼。我們有「開始」事件(針對第一個測試主題),「下一個主題」事件(針對後續測試主題)以及「跟蹤器」代碼。

下個主題:(這是一個我們需要進行更改)

/// <summary> 
    /// stops "old" file and graph streams, closes file, launches dialog for new subject and starts 
    /// stream up again 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void buttonNextSubject_Click(object sender, RoutedEventArgs e) 
    { 
     //Ask the user if they're sure they want to stop the current stream 
     if (MessageBox.Show("Are you sure you want to stop the stream and start a new one?", "New Subject", MessageBoxButton.OKCancel) == MessageBoxResult.OK){ 
      if (fileCheck == true) 
      { 
       tracker.outputFile.closeFiles(); 
       //SHUT DOWN OLD TRACKER HERE 
      } 
      else 
      { 
       return; 
      } 
     } 


     Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); 
     dlg.FileName = "Subject"; //Default file name 
     dlg.DefaultExt = ".txt"; //Default file extension 
     dlg.Filter = "Text documents (.txt)|*.txt"; //Filter files by extension 
     //Show save file dialog box 
     Nullable<bool> result = dlg.ShowDialog(); 
     buttonStart.IsEnabled = false; 
     buttonPause.IsEnabled = true; 
     buttonResume.IsEnabled = false; 
     fileCheck = true; 

     //Start Kinect- If no sensor, ask to connect one and exit. 
     try 
     { 
      this.sensor.Start(); 
     } 
     catch (IOException) 
     { 
      this.sensor = null; 
      //display error message here 
      MessageBox.Show("No Kinect sensor found. Please connect one and restart the application", "*****ERROR*****"); 
     } 
     sensor.ElevationAngle = 0; 
     tracker = new Tracker(sensor, this, dlg.FileName); 
    } 
} 

的跟蹤對象/跟蹤器類:

internal class Tracker{ 
     private Skeleton[] skeletons = null; 
     private MainWindow window; 
     public Class1 outputFile; 
     public string fn; 
     public Tracker(KinectSensor sensor, MainWindow win, string fileName){     
       //Connect the skeleton frame handler and enable skeleton tracking 
       sensor.SkeletonFrameReady += SensorSkeletonFrameReady; 
       sensor.SkeletonStream.Enable(); 
       sensor.ColorFrameReady += SensorColorFrameReady; 
       sensor.ColorStream.Enable(); 
       window = win;     
       outputFile = new Class1(fileName); 
       fn = fileName; 
       window.graphTickPen2.DashStyle = System.Windows.Media.DashStyles.Dash; 
     } 

最後,在「開始」事件,我基於「Next Subject」事件關閉:

private void buttonStart_Click(object sender, RoutedEventArgs e){   
     // Configure save file dialog box 
     Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); 
     dlg.FileName = "Default"; // Default file name 
     dlg.DefaultExt = ".txt"; // Default file extension 
     dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension 
     // Show save file dialog box 
     Nullable<bool> result = dlg.ShowDialog(); 
     buttonStart.IsEnabled = false; 
     buttonPause.IsEnabled = true; 
     buttonResume.IsEnabled = false; 
     fileCheck = true; 
     //Create drawing group 
     this.drawingGroup = new DrawingGroup(); 
     this.leftLegDrawingGroup = new DrawingGroup(); 
     this.rightLegDrawingGroup = new DrawingGroup(); 
     //Create image source for WPF control 
     this.imageSource = new DrawingImage(this.drawingGroup); 
     this.leftLegBoxImageSource = new DrawingImage(this.leftLegDrawingGroup); 
     this.rightLegBoxImageSource = new DrawingImage(this.rightLegDrawingGroup); 
     leftLegColorBitmap = new WriteableBitmap(483, 152, 96, 96, PixelFormats.Bgr32, null); 
     rightLegColorBitmap = new WriteableBitmap(483, 152, 96, 96, PixelFormats.Bgr32, null); 
     colorBitmap = new WriteableBitmap(640, 480, 96, 96, PixelFormats.Bgr32, null); 
     colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];   
     //Start Kinect- If no sensor, ask to connect one and exit. 
     try{ 
      this.sensor.Start(); 
     } 
     catch (IOException){ 
      this.sensor = null; 
      //display error message here 
      MessageBox.Show("No Kinect sensor found. Please connect one and restart the application", "*****ERROR*****"); 
     } 
     sensor.ElevationAngle = 0;    
     tracker = new Tracker(sensor, this, dlg.FileName); 
    } 
+2

當你幫助某人編碼問題時,你不要讓他們向你展示他們的代碼嗎?當然我們需要看代碼!!!! (只發布社區需要什麼幫助你,而不是整個項目) – Steven

回答

0

我只聽說過.dispose()到「termina一個類的實例「又名,處理它的資源分配。

0

一個不期而遇的Kinect的API會告訴你,你 以完全相同的方式使用KinectSensor.Stop(),你會覺得

reference to Kinect API

或者是你的問題,這個功能不會做被引用的是什麼?

編輯:我現在明白了這個問題,你可以從傳感器取消訂閱跟蹤器,像這樣:

sensor.SkeletonFrameReady -= tracker.SensorSkeletonFrameReady; 
sensor.ColorFrameReady -= tracker.SensorColorFrameReady; 

你也可以簡單地將事件在傳感器爲null,這將達到相同的(除了將取消從該事件的任何事件處理程序)

sensor.SkeletonFrameReady = null; 
sensor.ColorFrameReady = null; 

在這個例子中,這可能是更容易(因爲不會有問題時,跟蹤器不會被實例化和這樣的),如果你真的只讓追蹤器聆聽傳感器的流

+0

問題是,如果我們能夠提供幫助,我們不想「停止」傳感器。 – nerdenator

+0

你能否通過從訂閱傳感器中取消訂閱該跟蹤器來解決問題? 以秒爲單位編輯我的答案 – Doness