Matthew Flynn
Matthew Flynn

Reputation: 3941

Resetting System.Threading.Timer using .Change

Could someone please clarify the correct usage for System.Threading.Timer please.

I have a Server Client acknowledgement response model. What I am aiming to do is resend logged messages from the server, if no acknowledgment is received in after a set period of time.

So when the first message is sent I store a Timer;

AutoResetEvent autoEvent = new AutoResetEvent(false);
TestsTimeout timeout = new TestsTimeout(firstTest, _socket);

TimerCallback resendCallback = timeout.ResendMessage;
Timer timeoutTimer = new Timer(resendCallback, autoEvent, timeoutPeriod, timeoutPeriod);

Then when a acknowledgement to a message is received I simply want to reset the time back and send the next message out.

timeout.Change(0, timeoutPeriod);

Now, I followed the MSDN example for this, however, 0 seems to invoke the callback straight away? is this correct? what is the corect way of doing this please?

Upvotes: 1

Views: 1109

Answers (1)

usr
usr

Reputation: 171206

https://msdn.microsoft.com/en-us/library/yz1c7148(v=vs.110).aspx

Specify zero (0) to restart the timer immediately.

There you go.

Upvotes: 1

Related Questions