5

I have created one Windows Service where I need to execute the task on every day basis. so to accomplish that I have used Timer control.

Timer tm = new Timer();
tm.Elapsed += new ElapsedEventHandler(OnElapsedTime);
tm.Interval = 86400000;

But here the problem is, after I start the service, I have to wait for an entire 1 day to actually call that function.

So Is there a way where the function just been call at a time I start the service and don't have to wait till the interval time finishes.?

Thanks in advance.

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105

2 Answers2

6

You can run the method first time and then start the timer. So it will do the fist run and timer will run again in next day.

Or Create simple console application to do the task. simply by using windows scheduled task you can run this exe daily

Another option is call the event immediately after start the timer

How to fire timer.Elapsed event immediately

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153
  • Ok..that somehow works if I call the method for the first time but it's time taking method so it executes fine but when I start the service, it wait for few minutes and then says `"problem starting serivce, it didn't response in a timely manner"` – Vishal Suthar Jun 01 '13 at 06:52
  • Ok..That works for me..I want to make sure that the function I manually call will not be called again after an interval..so that will not be called twice..am I right.? – Vishal Suthar Jun 01 '13 at 07:03
  • @VishalSuthar if you start the service again that method will call again. What kind of task you doing? can't you update flag or something you can identify whether you run the task or not? – Damith Jun 01 '13 at 07:11
  • @VishalSuthar - If you call it manually, and run the timer, then it will be called at the point of manual invocation and also at the time when the timer interval elapses. What is your requirement? – Shakti Prakash Singh Jun 01 '13 at 07:14
  • @Damith Now the service just started normally and my manual function also called and finished without any error..but will that manual function again call when the interval elapses, I want it to only call the Elapsed function only and not the manual function from as of now.? – Vishal Suthar Jun 01 '13 at 07:17
  • @VishalSuthar No, manual call will run only you start service again – Damith Jun 01 '13 at 07:19
6

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);
Shakti Prakash Singh
  • 2,414
  • 5
  • 35
  • 59