mersadk
mersadk

Reputation: 337

Execute callback when timer is started in C#

Is there a way to execute timer callback for the first time at the moment the timer is started (Using start() method), and not to wait for interval to pass for the first execution?

UPDATE: I'm using System.Timers.Timer.

Upvotes: 0

Views: 1005

Answers (4)

Hans Kesting
Hans Kesting

Reputation: 39284

Try a different timer, like System.Threading.Timer. This supports an initial delay (you want a 0) plus a period.

Upvotes: 0

Nathan
Nathan

Reputation: 71

The 3rd parameter (1000) is the due time as your wish:

_timer = new System.Threading.Timer(new TimerCallback(InvokeTimerCallback), null, 1000, 3600000);

Upvotes: 0

Alvin Wong
Alvin Wong

Reputation: 12420

No, because the Timer is designed to call the event Tick after a certain interval it has started.

Instead, in your code you can manually call the event Tick when you first start the Timer.

P.S. I am talking about the WinForms Timer.

Upvotes: 0

paszczi
paszczi

Reputation: 306

A believe what you are looking for is described as dueTime here: http://msdn.microsoft.com/en-us/library/2x96zfy7.aspx

Upvotes: 2

Related Questions