I am programming some program that will generate statistics. For the recording of statistics I currently use a timer (set to 1000ms).
But it's not accurate. I have seen that after a while it is lower than the actual time. I think this has to do something with the CPU and stuff.
So my question: What is a good way to call a function every second?
By the way, this is what a part of my code looks like:
int totalSeconds = 0; // this number was taken from a savefile
int sessionSeconds = 0; // this number is for the session time
bool doUpdateGUI = true; // this is true when the form is shown
private void TimerMAIN_Tick()
{
totalSeconds++; // increase by 1
sessionSeconds++; // increase by 1
if (doUpdateGUI == true)
{
updateGUI(); // call the updateGUI void (which updates labels and textboxes)
}
}
Edit: I got the answer myself! I created it like this:
private void timerTClockEngine_Tick(object sender, EventArgs e) //this timer is set to 100ms
{
dt = DateTime.Now;
if (dt.Second != oldSec)
{
TClock_TICK();
oldSec = dt.Second;
}
}
private void TClock_TICK()
{
Ttime++;
textBoxT.Text = Ttime.ToString();
}