NibblyPig
NibblyPig

Reputation: 52922

C# Threading.Timer only ticks once

Very puzzled about timers, they don't seem to be working correctly.

I've created a timer like this:

this._catTimer = new Timer(state => this.catTimer_Tick(null, new EventArgs()), null, 0, Timeout.Infinite);

It ticks once immediately. At the end of the callback I have this:

this._catTimer.Change(5000, Timeout.Infinite);

But my timer never ticks again. This line is reached.

I've tried it with 5000, 0 too but it never ticks again. Any ideas?

Upvotes: 0

Views: 413

Answers (3)

NibblyPig
NibblyPig

Reputation: 52922

Have fixed this, it was caused by another thread that was blocking. You'd not think a threading timer would be affected by this but it was.

Upvotes: 0

Adil
Adil

Reputation: 148110

You are giving Timeout.Infinite for the period argument in both constructor (public Timer(TimerCallback callback, Object state, uint dueTime, uint period)) and Change( int dueTime, int period) method which mean you never want the periodic signalling.

period

The time interval between invocations of the callback method specified when the Timer was constructed, in milliseconds. Specify Timeout.Infinite to disable periodic signaling, MSDN.

Upvotes: 1

Naeem A. Malik
Naeem A. Malik

Reputation: 1025

Try putting a try/catch around the logic in catTimer_Tick method. Maybe your code is never reaching .Change line.

Upvotes: 0

Related Questions