“终止”类的实例

时间:2012-11-09 18:02:07

标签: c# oop kinect

美好的一天呃。

我正在开发一个C#项目,在这个项目中我们使用Microsoft Kinect,用它来跟踪执行一系列跳跃的人,并获取为计算膝角度而生成的骨架。

昨晚当我们测试主题时,我认为对程序进行控制可以点击并结束“当前”测试对象的输出文件,然后启动另一个主题。为了开始跟踪骨架,我们创建了一个Tracker类的跟踪器对象,可以在Kinect for Windows SDK中找到。然后启动跟踪和输出的所有逻辑。但是,当我们“关闭”第一个主题的输出时,我们似乎只关闭文件,而不是跟踪器对象的实际实例。这导致跟踪器保持“打开”,与我们为新测试对象创建的跟踪器对象的新实例一起使用,使用双倍的计算资源并导致性能的不可接受的降级。

如何“关闭”跟踪器对象的第一个实例?

如果你们需要代码来检查,请告诉我们;我没有把它包括在内,因为在这里张贴是不切实际的。

编辑:

这里至少有一些代码。我们有“开始”事件(对于第一个测试对象),“下一个对象”事件(对于后续测试对象)和“跟踪器”代码。

下一个主题:(这是我们需要进行更改的那个)

    /// <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;
        }

最后,我基于“下一个主题”事件的“开始”事件:

    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 个答案:

答案 0 :(得分:0)

我只听说过.dispose()来“终止一个类的实例”,即处理它的资源分配。

答案 1 :(得分:0)

看看Kinect API会告诉你的,  你完全按照你的想法使用KinectSensor.Stop()

reference to Kinect API

或者你的问题是这个函数没有做引用的内容吗?

编辑:我现在理解这个问题,你可以像这样从传感器中取消订阅跟踪器:

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

您也可以简单地将传感器中的事件设置为null,这将相同(除非它取消订阅该事件中的任何事件处理程序)

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

在这个例子中,这可能更容易(因为当没有实例化跟踪器等时没有问题),如果你真的只让跟踪器听取传感器的流