Sahar Zehavi
Sahar Zehavi

Reputation: 590

Timer doesn't restart after first interval

I'm trying to get a function working every X seconds and i'm using System.Timers for that task.

after the first time the function calls itself, the pogram ends instead of just continue to the next interval.. any way to do that instead of just using a loop? it seems like it will be the wrong use of the Timer.

my code is like so:

        Timer timer1 = new Timer();
        timer1.Elapsed += new ElapsedEventHandler(someFunction);
        timer1.Interval = 1000; // in miliseconds
        timer1.Start();

Upvotes: 1

Views: 152

Answers (2)

Georg
Georg

Reputation: 5791

Be aware that the Timer object instance might be garbage collected. Thus, you need to keep a reference to the timer as long as you need it. Otherwise the garbage collector will collect your Timer and release the unmanaged resources attached to it and thus deactivates the timer.

Upvotes: 2

Koscik
Koscik

Reputation: 189

Maybe try

timer.AutoReset = true;

for System.Timers.Timer class

Upvotes: 0

Related Questions