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条回答
淡お忘
2楼-- · 2019-01-18 17:56

Task.Run(() => { Timer_Elapsed(null, null); });

After Timer creation/configuration, worked fine for me...

查看更多
Deceive 欺骗
3楼-- · 2019-01-18 17:58

Here is the easy answer. (Assuming you're using a Windows form)

After dragging your timer control to your form, set the enabled property to true, and the Interval property to whatever you want the initial value to be. (100 is the default and will start the tick event immediately)

enter image description here

Then within your tick event, simply check the value of the Interval property. If it is not your desired value, set it. It's that simple. The same can be accomplished in your C# code if creating a timer on the fly.

enter image description here

查看更多
劫难
4楼-- · 2019-01-18 18:03

I have implemented in VB.NET with AddHandler

Public Class clsMain Inherits ServiceBase

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
gTimer.Interval = 1000  
gTimer.Enabled = True
AddHandler gTimer.Elapsed, AddressOf gTimer_Elapsed
End Sub

'When the timer is elapsed this event will be fired

Protected Sub gtimer_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs)
'Stop the timer and do some work
gTimer.Stop()
'Custom code
'Here
'Start to raise the elapsed event again
gTimer.Start()
End Sub
End Class
查看更多
我命由我不由天
5楼-- · 2019-01-18 18:06

For the first time, the timer will start after 1 second. After that, its interval will be changed to every 30 seconds or whatever...

    //main function    
    Timer timer = new Timer(1000);   //initial start after 1 second
        timer.Elapsed += new ElapsedEventHandler(TimerElapsedMethod);
        timer.Start();            
    }
    private void TimerElapsedMethod(object sender, ElapsedEventArgs e) 
    {
        Timer timer = (Timer)sender; // Get the timer that fired the event
        timer.Interval = 30000;      // Change the interval to whatever
        .
        .
        .
    }
查看更多
祖国的老花朵
6楼-- · 2019-01-18 18:08

I just called the **ElapsedEventHandler** with null parameters.

查看更多
We Are One
7楼-- · 2019-01-18 18:08

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

查看更多
登录 后发表回答