How to loop in Windows Service?

  • Thread starter Thread starter eb65
  • Start date Start date
E

eb65

I have a need to write a Windows Service application in VB.Net that
needs to continuously do some processing, wait ten minutes, then
repeat. What is a good approach to coding this type of thing?
Basically, I want something like:

While True
-- do some processing

-- sleep 10 minutes

End While

I can't put a loop like this in the OnStart event handler, because then
the service never finishes starting in MMC. I thought of using a Timer
instead of a Sleep, but I don't want the processing to occur every 10
minutes when the Timer.Elapsed event fires - I want a 10 minute pause
after the processing completes.

Has anyone implemented a similar application, or have any suggestions
as how this can best be done?

Thanks in advance for any replies.
 
try puting a timer... that fires only once... lets say... on start
event.. fires.. loads everything thats needs to be loaded... 10 seconds
later, your loop is started.. and timer i disabled... now you can loop
your way... after onstart event has finished.....

add second timer.. or do custom logic and use the sam... that will will
start 10 minute count down after your procesing is finished.....
 
Kick off your processing in a thread. Start the thread in the OnStart.
The thread can then loop as you wanted.
 
I have a need to write a Windows Service application in VB.Net that
needs to continuously do some processing, wait ten minutes, then
repeat. What is a good approach to coding this type of thing?
Basically, I want something like:

While True
-- do some processing

-- sleep 10 minutes

End While

I can't put a loop like this in the OnStart event handler, because then
the service never finishes starting in MMC. I thought of using a Timer
instead of a Sleep, but I don't want the processing to occur every 10
minutes when the Timer.Elapsed event fires - I want a 10 minute pause
after the processing completes.

Start processing in a new thread (see 'Thread' class and 'ThreadStart'
delegate). Then you can use 'System.Threading.Thread.Sleep' to block the
thread for a certain period of time.
 
eb65,
In addition to the other comments, I normally use either
System.Threading.Timer or System.Timers.Timer and handle the respective
callback or Elapsed event.

If you us System.Timers.Timer, be certain to review the following KB article
first:
BUG: The Elapsed event of the System.Timers.Timer class is not raised in a
Windows service
http://support.microsoft.com/default.aspx?scid=kb;en-us;842793


For info on the 3 timers (the above two & System.Windows.Forms.Timer) see:

Comparing the Timer Classes in the .NET Framework Class Library
http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I have a need to write a Windows Service application in VB.Net that
| needs to continuously do some processing, wait ten minutes, then
| repeat. What is a good approach to coding this type of thing?
| Basically, I want something like:
|
| While True
| -- do some processing
|
| -- sleep 10 minutes
|
| End While
|
| I can't put a loop like this in the OnStart event handler, because then
| the service never finishes starting in MMC. I thought of using a Timer
| instead of a Sleep, but I don't want the processing to occur every 10
| minutes when the Timer.Elapsed event fires - I want a 10 minute pause
| after the processing completes.
|
| Has anyone implemented a similar application, or have any suggestions
| as how this can best be done?
|
| Thanks in advance for any replies.
|
 
Chris said:
Kick off your processing in a thread. Start the thread in the OnStart.
The thread can then loop as you wanted.

this is good to know.... cose i'm too still learining... especialy to
make windows service.... my works for now...now i'll modify it with
suggestions made here :-)
 
Back
Top