Kinect加速度计数据计时器备份 [英] Kinect accelerometer data timer backup

查看:97
本文介绍了Kinect加速度计数据计时器备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是编程的新手,您可能会看到这一点,但这是我的问题: 我想记录Kinects加速度计数据,并每隔5分钟将其写入文件作为备份.但是我无法在Timers OnTimeEvent中找到StringBuilder数据,该数据包含我从加速度计获得的数据.所以问题是,是否可以将StringBuilder数据通过ElapsedEventArghs传递,或者是否有其他选择可以到达StringBuilders数据? 我在这里复制我的代码: 命名空间KinectSokadikTeszt {

First of all, I'm very beginner at programming and you probably will see that, but here is my problem: I want to record a Kinects Accelerometers data, and write it to a file every 5 minutes as a backup. But I can't reach in the Timers OnTimeEvent the StringBuilder data, that contains what the I get from the Accelerometer. So the question is, can the StringBuilder data passed trough the ElapsedEventArghs, or is there any other option, to reach the StringBuilders data? I copy my code here: namespace 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();
    }
}

}

推荐答案

您的代码因以下两个原因而无效:

Your code don't work for two reason:

1)流写入器在主体中声明,并且是一个局部变量,因此事件OnTimeEvent看不到它

1) The streamwriter is declared in the main and it's a local variable so the event OnTimeEvent can' t see it

2)CounterClass的指针也是如此.

2)The same thing for the pointer of CounterClass.

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

If you want pass the class pointer to the event you must do something like this:

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

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

And when you assign the event to the Timer you must do this:

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

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

EDIT: you can also use this that is a shorter version:

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

编辑2

我看过Main(string []args),但我不太喜欢.如果我是你,我会这样做:

I've seen the Main(string []args) and I don't like so much. I' ll do this if I were you:

 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中将布尔值设置为true,它将在文件末尾附加字符串.如果文件不存在,则布尔值将被丢弃,流写入器将创建该文件. 有关StreamWriter的更多信息,请查看这里

If you set true the bool in the StreamWriter it will append the string at the end of file. If file not exist the boolean will be discard and the streamwriter will create the file. For more information about StreamWriter take a look here

这篇关于Kinect加速度计数据计时器备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆