Kinect加速度计数据定时器备份

时间:2013-10-13 19:28:07

标签: c# timer kinect accelerometer

首先,我是编程的初学者,你可能会看到,但这是我的问题: 我想记录一个Kinects Accelerometers数据,并每隔5分钟将其写入一个文件作为备份。但我无法在Timers OnTimeEvent中找到StringBuilder数据,它包含了我从Accelerometer获得的内容。所以问题是,StringBuilder数据是否可以通过ElapsedEventArghs传递,还是有任何其他选项来访问StringBuilders数据? 我在这里复制我的代码: 名称空间KinectSokadikTeszt {

public class CounterClass 
{
    private int counter;

    public int Counter
    {
        get { return counter; }
        set { counter = value; }
    }
    private StringBuilder stringTemp;

    public StringBuilder StringTemp
    {
        get { return stringTemp; }
        set { stringTemp = value; }
    }
    public int GetCounter()
    {
        return counter;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        CounterClass sz = new CounterClass();
        KinectSensor sensor = KinectSensor.KinectSensors[0];
        Vector4 gravity;
        FileStream fs = new FileStream(@"c:\teszt.txt", FileMode.Create);
        fs.Close();
        StreamWriter sw = new StreamWriter(@"c:\teszt.txt");
        sz.StringTemp = new StringBuilder();
        sz.Counter = 0;
        Timer timer = new Timer();
        timer.Start();
        timer.Elapsed += new ElapsedEventHandler(OnTimeEvent);

        timer.Interval = 300000;
        timer.Enabled = true;


        while (sz.Counter < 7500)
        {
            gravity = sensor.AccelerometerGetCurrentReading();
            DateTime dt = DateTime.Now;
            Console.WriteLine("X: {0}\tY: {1}\tZ: {2}\t{3}.{4}", gravity.X, gravity.Y, gravity.Z, dt, dt.Millisecond);
            sz.StringTemp.AppendLine("X:" + gravity.X.ToString() + "\tY:" + gravity.Y.ToString() + "\tZ:" + gravity.Z.ToString() + "\t" + dt + "." + dt.Millisecond);
            sz.Counter++;
        }

    }

    private static void OnTimeEvent(object sender, ElapsedEventArgs e)
    {
        sw.Write(sz.StringTemp);
        sw.Close();
    }
}

}

1 个答案:

答案 0 :(得分:0)

您的代码无法正常工作:

1)编写器在main中声明,它是一个局部变量,因此事件OnTimeEvent看不到它

2)CounterClass的指针同样如此。

如果要将类指针传递给事件,则必须执行以下操作:

private static void OnTimeEvent(object sender, ElapsedEventArgs e, CounterClass cz)
{
    sw.Write(sz.StringTemp);
    sw.Close();
}

当您将事件分配给Timer时,您必须执行以下操作:

myTimer.Elapsed += new ElapsedEventHandler((sender, e) => OnTimeEvent(sender, e, cz));

编辑:您也可以使用更短的版本:

myTimer.Elapsed += (sender, e) => OnTimeEvent(sender, e, cz);

编辑2

我见过Main(string []args)并且我不喜欢这么多。如果我是你,我会这样做:

 public static void Main(string[] args)
{
    CounterClass sz = new CounterClass();
    KinectSensor sensor = KinectSensor.KinectSensors[0];
    Vector4 gravity;
    sz.StringTemp = new StringBuilder();
    sz.Counter = 0;
    Timer timer = new Timer();
    timer.Start();
    timer.Elapsed += (sender,e) => OnTimeEvent(sender,e,cz);
    timer.Interval = 300000;
    timer.Enabled = true;
    while (sz.Counter < 7500)
    {
        gravity = sensor.AccelerometerGetCurrentReading();
        DateTime dt = DateTime.Now;
        Console.WriteLine("X: {0}\tY: {1}\tZ: {2}\t{3}.{4}", gravity.X, gravity.Y, gravity.Z, dt, dt.Millisecond);
        sz.StringTemp.AppendLine("X:" + gravity.X.ToString() + "\tY:" + gravity.Y.ToString() + "\tZ:" + gravity.Z.ToString() + "\t" + dt + "." + dt.Millisecond);
        sz.Counter++;
    }

}
 private static void OnTimeEvent(object sender, ElapsedEventArgs e, CounterClass cz)
 {
    using(System.IO.StreamWriter sw=new System.IO.StreamWriter("myPath\\MyFileName.txt",true))
        sw.Write(sz.StringTemp);
 }

如果在StreamWriter中将bool设置为true,它会将字符串附加到文件末尾。如果文件不存在,则布尔值将被丢弃,并且流写器将创建该文件。 有关StreamWriter的更多信息,请查看here