How to fire timer.Elapsed event immediately

82,233

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...

Share:
82,233
Otiel
Author by

Otiel

Profile picture credits: http://codegolf.stackexchange.com/a/22326/14595

Updated on December 04, 2020

Comments

  • Otiel
    Otiel over 3 years

    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.

  • Otiel
    Otiel almost 13 years
    I should have specify that I don't want to use the System.Threading.Timers cause I develop a Windows service. Thx anyway :)
  • Yahia
    Yahia almost 13 years
    we use System.Threading.Timers in Windows service - are they not allowed or what kind of problems could happen in Windows Service ?
  • Otiel
    Otiel almost 13 years
    It 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
    Yahia almost 13 years
    thanks :-) 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
    Otiel almost 13 years
    Seems 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 for source? How can I create a new ElapsedEventArgs ?
  • George Duckett
    George Duckett almost 13 years
    The Tick Event of the System.Windows.Forms.Timer has a source and EventArgs parameter, the source would be the timer, just do new EventArgs() for the 2nd parameter. If you're using the System.Threading.Timer, then for the 2nd parameter just create a new ElapsedEventArgs, setting SignalTime to something like DateTime.Now().
  • Matten
    Matten almost 13 years
    OnTimer(timerInstance, new ElapsedEventArgs() { ... }); fill the ElapsedEventArgs-fields as you need them.
  • user1703401
    user1703401 almost 13 years
    Always favor System.Threading.Timer over System.Timers.Timer, it is a much better timer class.
  • Otiel
    Otiel almost 13 years
    @Matten Actually I can't - ElapsedEventArgs has no constructor defined. Am I missing a point?
  • Matten
    Matten almost 13 years
    @Leito yes, ElapsedEventArgs's ctor is marked as internal. So use null as second argument and in the handler code if(e==null) signalTime=DateTime.Now would yield the same result as initializing ElapsedEventArgs with signalTime=DateTime.Now (which isn't possible)
  • Otiel
    Otiel almost 13 years
    @Matten Works like a charm. Thanks!
  • Otiel
    Otiel almost 13 years
    @Hans Do you have some references? Links?
  • user1703401
    user1703401 almost 13 years
    @Leito - ask a question about it, that's the way it works around here.
  • bh_earth0
    bh_earth0 about 8 years
    System.Threading.Tasks.Task.Factory.StartNew( () => tmr_Elapsed(null, null) ); if you want the non-thread-blocking beahiour.
  • rollsch
    rollsch over 6 years
    Why was this down voted? This will work and answers the question with a working example.
  • Kim Homann
    Kim Homann over 2 years
    The question was about System.Timers.Timer, so there is no Tick.
  • Kim Homann
    Kim Homann over 2 years
    Best answer! Although you don't even need the _timer.Stop(); bc you set _timer.AutoReset = false;, so you enter OnTimer with _timer.Enabled set to false anyway. But it doesn't do any harm either.