To pause an Windows service application

T

Tony Johansson

Hi!

Here is some text from e-learning about windows services.
"If you want to pause and restart the Windows service application, you must
set the CanPauseAndContinue property to True. If this property is set to
True, you should override the OnPause and OnContinue methods.
However, if the OnPause method releases all the resources in the OnStart
method, it functions as the OnStop method. You can pause only a portion of
the work that the service perform while the service perform the remaining
activities normally."

Now to my question what in meant by the last row when it says
"You can pause only a portion of the work that the service perform while the
service perform the remaining activities normally."

I mean that if you for example use a timer in the OnStart where you set the
Enable proprty to true you might set the Enable proprty to false in the
OnPause and then in the OnContinue set the Enable property to true again.
This is how I understand how OnPause would work.

//Tony
 
J

Jeff Johnson

Here is some text from e-learning about windows services.
"If you want to pause and restart the Windows service application, you
must set the CanPauseAndContinue property to True. If this property is set
to True, you should override the OnPause and OnContinue methods.
However, if the OnPause method releases all the resources in the OnStart
method, it functions as the OnStop method. You can pause only a portion of
the work that the service perform while the service perform the remaining
activities normally."

Now to my question what in meant by the last row when it says
"You can pause only a portion of the work that the service perform while
the service perform the remaining activities normally."

Wow, yet another piece of bad wording from this book.

What it's saying here is ultimately some ridiculously obvious stuff: you
can't stop EVERYTHING in your service. Something must continue to execute at
some level or the service would not be able to come out of its paused state.
In other words, the Windows message loop is still going to run. Well, duh.
I mean that if you for example use a timer in the OnStart where you set
the Enable proprty to true you might set the Enable proprty to false in
the OnPause and then in the OnContinue set the Enable property to true
again.
This is how I understand how OnPause would work.

Yes, you have it right. Another thing you can do is to create a class-level
(or perhaps global) WaitHandle (like a ManualResetEvent) and sprinkle a
bunch of WaitOne() calls throughout your code. Then, in the OnPause event
you could Reset() the object (which will cause any call to WaitOne() to
block) and in the OnContinue event you can Set() it.
 
J

Jeff Johnson

In other words, the Windows message loop is still going to run.

I probably should have said "or whatever a service's equivalent is." I'm not
sure I've ever written a Windows service in C so I don't know if it actually
has a message pump or not, but you should get the gist of what I was saying
in my original reply.
 
J

Jeff Johnson

As you code what happens in OnPause/OnStart, IMO they just meant that you
can do pretty much what you want here...

For example, if your service has a request queue, when the service is
paused you could refuse new requests but keep processing already queued
requests (that is a "portion of the work" is paused while "remaining
activities" keeps going on).

You COULD do that but as a user I would not in any way, shape, or form
expect that to be what happens when a service is paused. I would expect all
"normal" work to stop and the only thing to conitnue working in the service
would be the mechanism to listen for a resume/continue signal.

Decent possible explanation, though.
 
J

Jeff Johnson

Then it means the service is stopped as it does nothing else rather than
waiting to start again. So pausing or stopping the service wouldn't make
any difference.

Sure it does! All the service's current state would still be in memory. Do
you think that using "Sleep" on your computer is the same as "Shut Down"?
 

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

Top