简体   繁体   中英

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?

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM