service coded in managed C++; how are restarts handled?

A

A Marlow

Hello,

I am new to this game so please bear with me. I have just written a simple
application as a service using Visual Studio and managed C++. I wish the
process to run in the background and to not use a GUI **ever**. It just
runs. And if it fails for ANY reason then it is simply to be restarted. I
need some help with this please.

I have already found about _CrtSetReportMode and how I need to call it to
suppress the popups I would otherwise get on application crash. But I am
not sure how to make it so that the application is restarted. If it
crashes the restart should be immediate. Also, if possible I would like it
logged in the event log. Please, how is it done?

Regards,

Andrew Marlow
 
W

William DePalo [MVP VC++]

A Marlow said:
I am new to this game so please bear with me.

You have come to a pretty civil place.
I have just written a simple application as a service
using Visual Studio and managed C++. I wish the
process to run in the background and to not use a GUI
**ever**. It just runs. And if it fails for ANY reason then
it is simply to be restarted. I need some help with this please.
OK.

But I am not sure how to make it so that the application is
restarted. If it crashes the restart should be immediate.

At the risk of stating the obvious, a dead application can't resurrect
itself. So you need some external agent to do that. You could write one but
then that begs the question as to what to do if it fails. :)

So, usually developers just install their services and leave the restart to
the service control manager. The properties dialog for every service has a
recovery tab wherein you can set the restart policy.

In native code, you can set the restart policy by calling
ChangeServiceConfig2(). I'm not sure how that is done in managed code.
Someone else may chime in here with that info if you are looking for a
managed solution.

Regards,
Will
 
G

Guest

William DePalo said:
You have come to a pretty civil place.

On most days, yes. ;)
In native code, you can set the restart policy by calling
ChangeServiceConfig2(). I'm not sure how that is done in managed code.
Someone else may chime in here with that info if you are looking for a
managed solution.

From what I can tell, the service restart policy has not yet been exposed
directly to the CLR. As I see it, you have two options:

1. Use P/Invoke on ChangeServiceConfig2(), and set the restart policy that
way, or,
2. Create a second small application or service, whose only purpose in life
is to monitor the first service and restart it if it should fail.

I'd prefer option 1, since you'd only have to set the policy once and you
wouldn't incur the overhead of a second application running.

Sean
 
A

apm35

William said:
At the risk of stating the obvious, a dead application can't resurrect
itself. So you need some external agent to do that.

I was hoping that Windoze would come with a working facility for this.
I found the recovery tab in service properties but I have had
difficulty in making it work.
So, usually developers just install their services and leave the restart to
the service control manager. The properties dialog for every service has a
recovery tab wherein you can set the restart policy.

When I tried I found the only thing that actually seems to cause it to
restart was by setting the fail count to zero and action on first
failure to restart, then setting the polling frequency to 1 minute.
With these set and with the service deliberately rigged to core dump,
it got restarted EVERY minute. Whilst this is what I want, I was
suprised, considering I told Windoze to only restart on *first*
failure.
In native code, you can set the restart policy by calling
ChangeServiceConfig2(). I'm not sure how that is done in managed code.
Someone else may chime in here with that info if you are looking for a
managed solution.

Hmm. I would like this to be done externally if poss, but thanks for
the tip.

Regards,

Andrew
 
G

Guest

Whilst this is what I want, I was
suprised, considering I told Windoze to only restart on *first*
failure.

Does it make you feel 'l337' to say 'windoze'?
;-)
 
W

William DePalo [MVP VC++]

I was hoping that Windoze would come with a working facility for this.

_Windows_ does come with a working facility for doing this both at a user
level and at a programmatic level.
I found the recovery tab in service properties but I have had
difficulty in making it work.

Work harder.

Regards,
Will
 
A

a_marlow

_Windows_ does come with a working facility for doing this both at a user
level and at a programmatic level.


Work harder.

I have. And it now works. Not quite the way I expected but I can get a
service to dynamically restart and not invoke any popups when it fails.
This is what I wanted. I had heard rumours that the facility doesn't work
but those rumours must be false.
 

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