0

首先,我是编程的初学者,您可能会看到,但这是我的问题:我想记录 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();
    }
}

}

4

1 回答 1

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

于 2013-10-13T20:21:14.370 回答