Is Windows Service what I need?

  • Thread starter Thread starter BobAchgill
  • Start date Start date
B

BobAchgill

I have a Form that on a timer uses FTP to update data on
the local disk.

I would like this Form to start on boot of the computer
and stay running still shutdown.

Like putting it in the startup folder?? Or is that old
Windows 98 terminology?

For an XP machine is Windows Service for this purpose? I
read some on another post that Windows Service does not
support Forms?!? Hmmmm?

If so... then how do I integrate my Form code with its
data processing into the Windows Service approach?

Thanks!

Bob
 
BobAchgill said:
I would like this Form to start on boot of the computer
and stay running still shutdown.

Do you really want it running

"Boot 'til shutdown"

or would

"Log-On 'til Log-Off"

do just as well?

If the former, go the Windows Service route.

If the latter, just pop a shortcut into the Startup Group.

HTH,
Phill W.
 
I think "Boot 'til shutdown". I need to have the
function working regardless of who is logged on.

Though, never did find the "startup group" on my XP Pro.
Where would that be hiding?

So that leaves the question...

Where do I stick in my FTP processing code??

Here is what I see in my Window Service code so far...
where do I put my code?

+++++++++++++++++++++++++++++++++
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This
method should set things
' in motion so your service can do its work.
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down
necessary to stop your service.
End Sub
+++++++++++++++++++++++++++++++++
 
BobAchgill said:
So that leaves the question...

Where do I stick in my FTP processing code??

OnStart gets called when your service is started and returning from
this method signals to the Service Control Manager that your service
has started successfully. What I'd suggest is a System.Timers.Timer
that fires once and only once that you start in OnStart.

When this timer fires, call your main processing routine that can do
whatever it needs to, but I'd recommend using Thread.Sleep() to
leave a gap in processing; don't use the Timer (I've had them stop
on me for no readily apparent reason).

OnStop gets called when your service is asked to stop and returning
from this method says to the Service Control Manager "you can kill
this one now"; beware if your processing could be interrupted part
way through!

(a starting point)

Private m_bShutDownComplete as Boolean = False
Private m_bShutDownRequested as Boolean = False

Sub OnStart()
Me.tmrStarter.Enabled = True
End Sub

Sub OnStop()
m_bShutDownComplete = False
m_bShutDownRequested = True
Do While m_bShutDownComplete
Thread.Sleep( 500 )
Loop
End Sub

Sub tmrStarter_Elapsed( ... ) _
Handles tmrStarter.Elapsed

Me.tmrStarter = False
RunProcess()

End Sub

Sub RunProcess()
Do While Not m_bShutDownRequested
' Do Something Useful
. . .
Thread.Sleep( 500 ) ' or whatever
Loop
m_bShutDownComplete = True
End Sub

HTH,
Phill W.
 
Phil,

Thank you so much for clearing up how this Windows
Service stuff works with real code.

What you wrote really looks great.

Once question... why did you start it off with a timer...

Sub OnStart()
Me.tmrStarter.Enabled = True

as opposed to calling the process right off ...

Sub OnStart()
RunProcess()

Bob
 
BobAchgill said:
Once question... why did you start it off with a timer...

Sub OnStart()
Me.tmrStarter.Enabled = True

as opposed to calling the process right off ...

Sub OnStart()
RunProcess()

Imagine starting the Service manually, using Control Panel.

You click "Start".
(Your OnStart routine gets called).
Control Panel puts up its progress bar to say "Starting Service"
and quietly ticks away to itself - and will continue to do so until
either the OnStart routine finishes or 30 seconds goes by, when
it gives up and says "I couldn't start the rotten thing" or words to
that effect).

If your OnStart routine ploughs straight into RunProcess(),
OnStart doesn't "finish" until the Service is stopped! By using
the Timer, your code "side-steps" this Call/Return chain, so
OnStart returns almost immediately and Control Panel sees the
Service as started.

So far, so good.

Then, a few milliseconds later, your Timer fires (just the once)
and the Real Work begins.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top