How to fire timer.Elapsed event immediately

2019-01-18 17:24发布

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.

标签: c# events timer
9条回答
Ridiculous、
2楼-- · 2019-01-18 18:09

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.

查看更多
戒情不戒烟
3楼-- · 2019-01-18 18:12

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();
}
查看更多
叛逆
4楼-- · 2019-01-18 18:14

If you want to be able to raise the event whenever you want (not only just at the moment you start the timer), you can encapsulate a timer in your own MyTimer class. This class exposes the original Timer methods and properties. Furthermore I added an event with explicit add and remove. In this way whenever you add a delegate to the event this is added to both the private MyTimer's event and to the original timer Elapsed event. This means that the timer triggers Elapsed in the usual way, but you can manually trigger the event calling RaiseElapsed (this should sound much simpler looking at the code).

public class MyTimer
{
    Timer t = new Timer();
    event ElapsedEventHandler timerElapsed;

    public event ElapsedEventHandler Elapsed
    {
        add
        {
            t.Elapsed += value;
            timerElapsed += value;
        }
        remove
        {
            t.Elapsed -= value;
            timerElapsed -= value;
        }
    }

    public double Interval
    {
        get
        {
            return t.Interval;
        }
        set
        {
            t.Interval = value;
        }
    }

    public void Start()
    {
        t.Start();
    }

    public void Stop()
    {
        t.Stop();
    }

    public void RaiseElapsed()
    {
        if (timerElapsed != null)
            timerElapsed(null, null);
    }
}
查看更多
登录 后发表回答