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) The streamwriter is declared in the main and it's a local variable so the event OnTimeEvent can' t see it
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();
}
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);
EDIT 2
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);
}
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