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