Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raising a function by timer

Tags:

c#

.net

timer

I want to raise a function periodically . When I finish one function cycle to wait some period of time and only them to start the second run.

I thought to make it like :

        timer = new System.Timers.Timer();
        timer.Interval = 1000;
        timer.Enabled = true;
        timer.Start();
        timer.Elapsed += TimerTick;


private void TimerTick(object sender, EventArgs e)
{
//My functionality
}

But seems that TimerTick is raised every secound and not secound from my last TimerTick run .

How i can solve this one ?

like image 560
Night Walker Avatar asked Apr 15 '26 00:04

Night Walker


1 Answers

You can use threads:

var thread = new Thread(o => {
    while(true)
    {
        DoTick();
        Thread.Sleep(1000);
    }
});
like image 180
Anton Gogolev Avatar answered Apr 17 '26 13:04

Anton Gogolev