Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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 that's it
}

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 //isClock
    {
        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;
}
Posted
Updated 19-Feb-17 4:40am
v8
Comments
Ralf Meier 19-Feb-17 5:53am    
I would not work like this because doing it in this way will cause a large Time-Jitter (like you described above).
Why don't you work with System-Parts like (for example) System.Datetime.Now ?

Perhaps there is a better approach to be found if you describe what you are trying to achieve ...
_Q12_ 19-Feb-17 6:01am    
thank you for your reply!
All I want is a highly customizable timer application, and this was my first timer application in my life - and is working as I wanted (except for the lag). Your suggestion is to make another code around Datetime class? I didnt think about it when I started... well, maybe for a second but I choose what I coded already. It is an interesting idea. I will think about it.
Ralf Meier 19-Feb-17 6:11am    
What should generelly should know about Events is :
Perhaps an Event is raised/fired a the right moment - but the Application works with it when there is time for it - in worst case it could be seconds (or perhaps also minutes) later.
So of course you could created your own timer-application (? task-scheduler ?) but what I suggest is : with the first call of it memorize the Now-Timestamp an in the following time work with resulting Timespan between Now and this memorized Timestamp ...
_Q12_ 19-Feb-17 6:23am    
aaa... maybe a pseudo-code will help? I did not understand your explanation. Sorry.
Ralf Meier 19-Feb-17 6:35am    
Yes a pseudo code would help ...
But again : what is the final goal ? I think I could be much more specific if you are also more specific ...

 
Share this answer
 
Comments
_Q12_ 19-Feb-17 10:12am    
I will repeat my previews answer to you here:
you said:
"Store the DateTime when the timer starts and subtract the elapsed DateTime... It is in the documentation ... also, google it and there are dozens of examples of how...
"
my response:

Thank you (Graeme_Grant)!
Very Interesting!
But How?
here is a quick pseudocode:

textBox1_TextChanged
{
s = datetime.s
request =6;
recalculate();
}
s = 0; //s = datetime.s in TextChanged
end=0;
request =6;
void recalculate()
{
end=s-request;
}
private void timer1_Tick(object sender, EventArgs e)
{
s--;
if (s= end)
{
timer.stop
}

}

Now I am looking over the samples from your link.
I find something interesting already that i did not know:
DateTime endTime = new DateTime(2013,01,01,0,0,0);
TimeSpan ts = endTime.Subtract(DateTime.Now); //?!^!#@!
I will have to test these new methods and approaches.
This code is just wrong:
C++
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 that's it
}

Why don't you use the RTC (Real Time Clock) ?
When you start to count
C++
start= DateTime.Now;

When you want to know the elapsed time
C++
now= DateTime.Now;
elapsed= now.Subtract(start); // in seconds

set a timer only to refresh the display or what you need ?
C library function - time()[^]
DateTime.Now Property (System)[^]
DateTime.Subtract Method (DateTime) (System)[^]
 
Share this answer
 
v3
Comments
_Q12_ 19-Feb-17 10:03am    
I think your time function is for c/c++.
This is c#. I am not that advanced(or smart) to steal from c++ and adapt to c#. Probably is a very good idea but I am the problem in this case. :) Thank you anyway.
Patrice T 19-Feb-17 10:42am    
oops
I was absolutely certain about C.
Patrice T 19-Feb-17 11:01am    
Should be better now.
Richard MacCutchan 19-Feb-17 10:04am    
In C#?
_Q12_ 19-Feb-17 10:31am    
ihi
See DateTime Structure (System)[^], TimeSpan Structure (System)[^] and Stopwatch Class (System.Diagnostics)[^].

All of the above can be utilised to achieve what you want.
 
Share this answer
 
Comments
_Q12_ 19-Feb-17 10:33am    
Thank you! I must experiment with them.
Google search spits out tons of examples... Here: Creating a simple Stopwatch/Timer application with C# / Windows Forms[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900