Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer_tick in c#

Tags:

c#

How can I set my timer to work with seconds? When I use the timer from toolbox, without any changes it starts working with another time unit.
I will be grateful for any help which you can give me.

I have something like this:

t = 0; 
timer1.Start(); 
if (t == 600) 
    timer1.Stop(); 
like image 752
Bogdan Avatar asked Jul 02 '26 18:07

Bogdan


2 Answers

EDIT Use timer.interval = 1000 * n; where n is the number of seconds between the ticks.

like image 163
Loci Avatar answered Jul 04 '26 06:07

Loci


Timer.Interval property takes the value in milliseconds. You should multiply your valued to 1000 to set the interval to seconds.

aTimer.Interval = 1*1000; // 1 second interval
aTimer.Interval = 2*1000; // 2 seconds interval

Edit:

If I understood correctly, you should register Timer.Tick event like

aTimer.Tick += new EventHandler(TimerEventProcessor);

and check the value of t in its event handler. If t == 600 then you can stop the timer

private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
    ...
    t++;
    if(t == 600)
       aTimer.Stop();
}
like image 26
ABH Avatar answered Jul 04 '26 06:07

ABH