471,627 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

Help:windows Service and Timer

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?

*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '05 #1
6 2902
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

Dec 7 '05 #2

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?
*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '05 #3
Hi,

You can create a scheduled job to start the service

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Marie-Christine Bechara" <ma*********************@ifsal.com> wrote in
message news:%2******************@TK2MSFTNGP14.phx.gbl...

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?
*** Sent via Developersdex http://www.developersdex.com ***

Dec 7 '05 #4


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?

*** Sent via Developersdex http://www.developersdex.com ***
Dec 7 '05 #5
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.

Dec 7 '05 #6
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

Dec 8 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Kurt Skaronea | last post: by
1 post views Thread by David A. Osborn | last post: by
5 posts views Thread by quortex | last post: by
reply views Thread by =?Utf-8?B?c2F2?= | last post: by
2 posts views Thread by dnuthtsaso | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.