There are 2 ways of doing it.
1) The code that is there in the OnElapsedTime
, put that in another method and call that method as soon as you start the timer. For eg, Put the code in TimerCalled
method and then use this:
static void Main(string[] args)
{
Timer tm = new Timer();
tm.Elapsed += new ElapsedEventHandler(OnElapsedTime);
tm.Interval = 86400000;
TimerCalled();
}
private static void OnElapsedTime(object source, ElapsedEventArgs e)
{
TimerCalled();
}
2)You can use the System.Threading.Timer
class instead. You can use it like this:
System.Threading.Timer tm =
new System.Threading.Timer(OnElapsedTime, null, 0, 86400000);
But be aware that the System.Threading.Timer
runs on a separate thread. Hence any attempt to update the UI elements
from that thread would result in error. You should rather delegate the UI elements updates, if any, to the UI thread via Dispatcher
or some other means.
UPDATE:
If you want to start a few minutes after the service starts, you could provide a delay of couple of minutes:
System.Threading.Timer tm =
new System.Threading.Timer(OnElapsedTime, null, 120, 86400000);