I build a timer application to count down and count up the time.
But when I compare it with other timer software I have, my timer is slower.
My best guess is because the timer event is containing too much stuff to do in 1 second. Too many if's.
a)-I think, the best approach to this issue is to simply make this:
int h1 = 0,h2 = 0;
int m1 = 0;m2 = 0
int s1 = 0;s2 = 0;
private void timer1_Tick(object sender, EventArgs e)
{
h1--;m1--;s1--;
h2++;m2++;s2++;
}
and then, update the form1 somehow, but not interfering with timer event.
b)-One idea is to create another timer (timer2), and in it's tick event to update the form1...but I am worrying if the timer2 will slow down the timer1?
c)-Another idea is to make two(2) threads to run the decrementing in one thread and updating UI in another thread.
d)-Another idea is to "paint" the UI after the timer is doing a minimal routine. I can easily make a bool became false when the tick occurs and in a "Paint" event to execute all the decrementing and all the if's, and set that bool to true. For that I have to make an event for a bool and that is over my ears...
I am hoping it is a easy solution to this problem, and before jumping to code, I think is best to ask first and get some opinions.
Thank you!
What I have tried:
My original code here:
int h = 0;
int m = 0;
int s = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (isTickActive) Ticking();
if (isTimer)
{
s = s - 1;
if (s == -1)
{
m = m - 1;
s = 59;
}
if (m == -1)
{
h = h - 1;
m = 59;
}
if (h == -1)
{
h = m = s = 0;
}
if (h == 0 && m == 0 && s == 0)
{
timer1.Stop();
button1.Text = "Start";
labelinfo.Text = "finished";
Ledstop = true; LedColor();
if (isAlarmActive) SoundTheAlarm();
}
}
else
{
s = s + 1;
if (s == 60)
{
m = m + 1;
s = 0;
}
if (m == 60)
{
h = h + 1;
m = 0;
}
if (h == 24)
{
h = m = s = 0;
timer1.Stop();
button1.Text = "Start";
labelinfo.Text = "finished";
Ledstop = true; LedColor();
}
}
label1.Text = s + "";
label2.Text = m + "";
label3.Text = h + "";
if (s < 10) label1.Text = "0" + s;
if (m < 10) label2.Text = "0" + m;
if (h < 10) label3.Text = "0" + h;
}