How to fire timer.Elapsed event immediately
Solution 1
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.
Solution 2
I just called the **ElapsedEventHandler**
with null parameters.
Solution 3
I know this answer is late but if you want your System.Timers.Timer
to be fired within 100ms (default interval) then you could simply just initialize the Timer
object without a specified interval, then set the interval within the called function to whatever you like. Here is an example of what I use in my Windows Service:
private static Timer _timer;
protected override void OnStart(string[] args)
{
_timer = new Timer(); //This will set the default interval
_timer.AutoReset = false;
_timer.Elapsed = OnTimer;
_timer.Start();
}
private void OnTimer(object sender, ElapsedEventArgs args)
{
//Do some work here
_timer.Stop();
_timer.Interval = 50000; //Set your new interval here
_timer.Start();
}
Solution 4
not sure about System.Timers.Timer
but try
System.Threading.Timer T = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 30000);
This starts immediately (0 milliseconds for first run, 30000 milliseconds for subsequents runs)...
see
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
http://msdn.microsoft.com/en-us/library/2x96zfy7.aspx
Solution 5
Task.Run(() =>
{
Timer_Elapsed(null, null);
});
After Timer creation/configuration, worked fine for me...
Otiel
Profile picture credits: http://codegolf.stackexchange.com/a/22326/14595
Updated on December 04, 2020Comments
-
Otiel over 3 years
I'm using the
System.Timers.Timer
class to create a timer with anTimer.Elapsed
event. The thing is theTimer.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. -
Otiel almost 13 yearsI should have specify that I don't want to use the
System.Threading.Timers
cause I develop a Windows service. Thx anyway :) -
Yahia almost 13 yearswe use System.Threading.Timers in Windows service - are they not allowed or what kind of problems could happen in Windows Service ?
-
Otiel almost 13 yearsIt is written on msdn: System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
-
Yahia almost 13 yearsthanks :-) that's what I read too... so there is nothing prohibiting the use in a Windows Service, only that there is another option with more features.
-
Otiel almost 13 yearsSeems the solution for me. But if I want to deal with the Tick callback method's parameters, what should I use?
OnTimer(Object source, ElapsedEventArgs e)
My timer instance forsource
? How can I create a newElapsedEventArgs
? -
George Duckett almost 13 yearsThe
Tick
Event of theSystem.Windows.Forms.Timer
has asource
andEventArgs
parameter, the source would be the timer, just donew EventArgs()
for the 2nd parameter. If you're using theSystem.Threading.Timer
, then for the 2nd parameter just create a new ElapsedEventArgs, settingSignalTime
to something likeDateTime.Now()
. -
Matten almost 13 years
OnTimer(timerInstance, new ElapsedEventArgs() { ... });
fill the ElapsedEventArgs-fields as you need them. -
user1703401 almost 13 yearsAlways favor System.Threading.Timer over System.Timers.Timer, it is a much better timer class.
-
Otiel almost 13 years@Matten Actually I can't -
ElapsedEventArgs
has no constructor defined. Am I missing a point? -
Matten almost 13 years@Leito yes,
ElapsedEventArgs
's ctor is marked as internal. So usenull
as second argument and in the handler codeif(e==null) signalTime=DateTime.Now
would yield the same result as initializingElapsedEventArgs
withsignalTime=DateTime.Now
(which isn't possible) -
Otiel almost 13 years@Matten Works like a charm. Thanks!
-
Otiel almost 13 years@Hans Do you have some references? Links?
-
user1703401 almost 13 years@Leito - ask a question about it, that's the way it works around here.
-
bh_earth0 about 8 yearsSystem.Threading.Tasks.Task.Factory.StartNew( () => tmr_Elapsed(null, null) ); if you want the non-thread-blocking beahiour.
-
rollsch over 6 yearsWhy was this down voted? This will work and answers the question with a working example.
-
Kim Homann over 2 yearsThe question was about
System.Timers.Timer
, so there is noTick
. -
Kim Homann over 2 yearsBest answer! Although you don't even need the
_timer.Stop();
bc you set_timer.AutoReset = false;
, so you enterOnTimer
with_timer.Enabled
set tofalse
anyway. But it doesn't do any harm either.