Help:windows Service and Timer

  • Thread starter Thread starter Marie-Christine Bechara
  • Start date Start date
M

Marie-Christine Bechara

I have a Windows Service project in C#.net and a timer during which a
certain function must be called.
Timer interval is say 3 minutes.
The function in the timer_Elapsed event.

Problem is when i start the service, the timer is directly firing. I
want the timer event to be fired after 3 minutes of starting the
service. how do i do that?
 
Although there might be some other ways to do this, the easiest way is
declaring a bool flag to determine if this is the first time the event
is called.

private bool isFirstCall = true;

In timer_Elapsed:
if (isFirstCall)
{
isFirstCall = false;
return;
}

// Do you stuff here

Hope it helps,
Thi
 
I have another problem: i want the service to start at a specific time
for example @4:00 AM. I cannot set it to 24 hours cause what if the
service stopped and started again in the same day.
How is that?
 
Hi,

You can create a scheduled job to start the service

cheers,
 
By service I mean Windows service in C#.net.
If time interval is 24hours (starting from 4:00 AM) and
windows service stopped @ 6:00 AM, then if i restart it @6:00AM, instead
of entering the time event next time it is 4:00 AM, it will enter it
next time @6:00AM.

I don't want that. how can i resolve this issue?
 
In your windows service you can still view and access the system clock.
Have your service fire off it's system timer every x number of seconds
and then check the system clock. If it's > 4am and < 10 am then do
your work, if doing your work after 10am then stop doing your work.

Regardless of when your service is stopped / started you can use the
system clock to determine when to run this way your service can run
24/7.

The only drawback to .net services is the rather nasty amount of memory
they consume to do relatively minute tasks.
 
If I understand it correctly you want an method to be invoked at a
specific time (4 am) daily. If your need is more complex, you might
need to write a scheduler which constantly check system time on a
background thread and invoke registered callbacks. In your case, you
could use the same approach in my previous post:
- When init the timer, set it interval to the timespan from now to next
4pm
- On the first ellapsed event invocation, reset the interval to 24h

In your init routine:
DateTime now = DateTime.Now;
DateTime today4PM = new DateTime(now.Year, now.Month, now.Day);
today4PM.Hour = 4;
DateTime next4PM = today4PM;
if (now > today4PM) next4PM = next4PM.AddDays(1);
Timer theTimer = new Timer();
theTimer.Interval = (next4PM - now).Millisecond;
theTimer.Start();

In timer_Elapsed:
if (isFirstCall)
{
isFirstCall = false;
// Reset interval to 24h
theTimer.Interval = 1000*60*60*60*24 // one day
}

// Do your stuff

Regarding your first problem, I have just looked at the documetation of
System.Timers.Timer and it says the event is not fired immediately. The
first time the event is invoked is when the Interval ellapsed. Besure
you set Interval *before* calling Start (or set Enabled to true)
because the default Interval is 100 milliseconds.

Thi
 
Back
Top