In a Windows Service how do I fail an OnStop() method

M

marccruz

Hi,

I am writing a Windows Service in C#. I want to gracefully fail the
"protected override void OnStop()" function. To do this, I first tried
throwing an Exception in the function. However, that left the Service
in a "Stopping" state.

I would like to fail the Service in the OnStop function without putting
the service in the Stopping state. It would be nice if it's still in
the Started state. Is this possible? If so, how?

Thanks
 
G

Guest

Not sure exactly what you mean by "gracefully fail". If the service fails or
an unhandled exception is throw, the service will stop. End of story!
Maybe you could provide a bit more detail about what the real goal is?
Peter
 
M

marccruz

Thanks for everyone's input,

Here's my real goal.

I want to install a service that monitors a program I wrote.

If the program is in a "Safe" state the service can be stopped
normally.

If the program is in an "Unsafe" state the service should not be
allowed to stop ("gracefully fail"). If this happens what I want to do
is:

- display a message box asking the user to put the program into a
"Safe" state before stopping the service.
- after the user puts the program into a "Safe" state, the user can
stop the service.

What is happening in my current implementation (by setting this.CanStop
= false or throwinng an exception in OnStop) is:

- the service does not stop
- the service is in a "Stopping" state

Because the service is in a "Stopping" state, the user can't put the
program into a "Safe" state then try to stop the service again. What
the user would have to do is to put the program into a "Safe" state,
then reboot the machine so he can restart the service.

I really want to know if there's a way [to fail the OnStop function
without putting the service in a "Stopping" state yet allowing the
service to be stopped if the state if the system is correct].

Thanks
 
A

Andrew

I really want to know if there's a way [to fail the OnStop function
without putting the service in a "Stopping" state yet allowing the
service to be stopped if the state if the system is correct].

I wonder if you might somehow self-restart from the onstop.. If there's
some way for the onstop to communicate to the onstart that the user had
just tried to stop the service with the program in an unsafe state.. Then
the onstart could handle telling the user to put the program into a safe
state before stopping the service..?

Just a thought.. I really have no idea if it's possible or if it would be
what you want.

Cheers

- Andrew
 
M

marccruz

It's something that would serve my requirement. I'll go ahead and try
it.
Andrew said:
I really want to know if there's a way [to fail the OnStop function
without putting the service in a "Stopping" state yet allowing the
service to be stopped if the state if the system is correct].

I wonder if you might somehow self-restart from the onstop.. If there's
some way for the onstop to communicate to the onstart that the user had
just tried to stop the service with the program in an unsafe state.. Then
the onstart could handle telling the user to put the program into a safe
state before stopping the service..?

Just a thought.. I really have no idea if it's possible or if it would be
what you want.

Cheers

- Andrew
 
M

marccruz

I tried doing an:

OnStart(...) inside my OnStop(). The service stopped so it doesn't
serve the requirement.

Is there a way of "gracefully failing" the OnStop() function in the
Service?
Do other programs try to do what I want to do?

Thanks
It's something that would serve my requirement. I'll go ahead and try
it.
Andrew said:
I really want to know if there's a way [to fail the OnStop function
without putting the service in a "Stopping" state yet allowing the
service to be stopped if the state if the system is correct].

I wonder if you might somehow self-restart from the onstop.. If there's
some way for the onstop to communicate to the onstart that the user had
just tried to stop the service with the program in an unsafe state.. Then
the onstart could handle telling the user to put the program into a safe
state before stopping the service..?

Just a thought.. I really have no idea if it's possible or if it would be
what you want.

Cheers

- Andrew
 

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