Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using the System.Timers.Timer class to create a timer with an Timer.Elapsed event. The thing is the Timer.Elapsed event is fired for the first time only after the interval time has passed.

Is there a way to raise the Timer.Elapsed event right after starting the timer ?

I couldn't find any relevant property in the System.Timers.Timer class.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
825 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just call the Timer_Tick method yourself.


If you don't want to deal with the Tick callback method's parameters, then just put the code that was in your Timer_Tick into another method, and call that from the Timer_Tick and from just after the Timer.Start() call


As pointed out by @Yahia, you could also use the System.Threading.Timer timer, which you can set to have an initial delay to 0. Be aware though, that the callback will run on a different thread, as opposed to the callback on the Windows.Forms.Timer which runs on the UI thread. So if you update any UI controls using the System.Threading.Timer (without invoking correctly) it'll crash.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...