Stop and start windows service programatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am working at .net 1.1, writing in c#.
I have windows service with a COM object.
Every unexpected time The COM object throw an error that make my service get
stuck (do not respond).
I can catch this error.
I want to restart my windows service every time the COM object throws an
error.

I use System.ServiceProcess.ServiceController to stop and start my service.
But there is one thing I do not understand:
When I write in the service code:

Code:
MyController.Stop()
MyController.Start()

How can the service reach the second line if I stop it?
Is it a good way to start and stop my service or should I use other way?

Thanks in advance!
 
shai said:
I am working at .net 1.1, writing in c#.
I have windows service with a COM object.
Every unexpected time The COM object throw an error that make my service get
stuck (do not respond).
[You really should catch the error locally, and don't let it to stop
your service.

like this:
try
{
comObject.DoSomething();
}
catch(COMException ex)
{
.....
}


try not to let it stop your service if you need to restart it anyway.

]


I can catch this error.
I want to restart my windows service every time the COM object throws an
error.

I use System.ServiceProcess.ServiceController to stop and start my service.
But there is one thing I do not understand:
When I write in the service code:

Code:
MyController.Stop()
MyController.Start()

How can the service reach the second line if I stop it?

[You use the service control to start the service, the service did NOT
start itself, so what's your confusion here .

For example, you start your car after you stop it, the car did NOT start
itself.]
 
Jianwei Sun,

First thank you for you’re detailed answer.
My confusion comes because I think that the serviceController is a part of
the service (because I declare it, in the service code and then stop it).

Does the service controller keep work after I stop the service (as I said I
declare the service controller in the service code)?

Shai.


Jianwei Sun said:
shai said:
I am working at .net 1.1, writing in c#.
I have windows service with a COM object.
Every unexpected time The COM object throw an error that make my service get
stuck (do not respond).
[You really should catch the error locally, and don't let it to stop
your service.

like this:
try
{
comObject.DoSomething();
}
catch(COMException ex)
{
.....
}


try not to let it stop your service if you need to restart it anyway.

]


I can catch this error.
I want to restart my windows service every time the COM object throws an
error.

I use System.ServiceProcess.ServiceController to stop and start my service.
But there is one thing I do not understand:
When I write in the service code:

Code:
MyController.Stop()
MyController.Start()

How can the service reach the second line if I stop it?

[You use the service control to start the service, the service did NOT
start itself, so what's your confusion here .

For example, you start your car after you stop it, the car did NOT start
itself.]
Is it a good way to start and stop my service or should I use other way?

Thanks in advance!
 
|I am working at .net 1.1, writing in c#.
| I have windows service with a COM object.
| Every unexpected time The COM object throw an error that make my service
get
| stuck (do not respond).
| I can catch this error.
| I want to restart my windows service every time the COM object throws an
| error.
|
| I use System.ServiceProcess.ServiceController to stop and start my
service.
| But there is one thing I do not understand:
| When I write in the service code:
|
|
Code:
| MyController.Stop()
| MyController.Start()
|
|
| How can the service reach the second line if I stop it?
| Is it a good way to start and stop my service or should I use other way?
|
| Thanks in advance!
|

Your real problem won't get solved by restarting your service. You have to
find out what's the cause of the COM error (an exception?).
Main cause of such errors is a COM object that runs in a context for which
it was not designed, like ActiveX controls, or COM objects that are called
from incompatible apartments.
Willy.
 
Willy,

I use the COM object as it is, because I do not have its code, and I can not
fix it.
The COM Exception I talk about happens ones in a week.
I set the properties of the service to restart on failure so when COM
Exception that make the service fall happens it restarts and works well.

This specific exception does not stop the service and visually (in the event
viewer) it looks like the service works, but it does not.
I know that restart the service after an error like this will solve the
problem, because this Exception is not something permanent (happens once a
week), and after restarting it manually it works well.
That’s why I want to restart it automatically programmatically.

1) Do you think that service controller is a good way?
2) Will the service controller continue work after I stop the service (I
declare the service controller in the service)?

Thanks in advance.
 
In this case, I think you need to write another service called
"MyServiceWatcher" which will start the service for you.
Willy,

I use the COM object as it is, because I do not have its code, and I can not
fix it.
The COM Exception I talk about happens ones in a week.
I set the properties of the service to restart on failure so when COM
Exception that make the service fall happens it restarts and works well.

This specific exception does not stop the service and visually (in the event
viewer) it looks like the service works, but it does not.
I know that restart the service after an error like this will solve the
problem, because this Exception is not something permanent (happens once a
week), and after restarting it manually it works well.
That’s why I want to restart it automatically programmatically.

1) Do you think that service controller is a good way?
2) Will the service controller continue work after I stop the service (I
declare the service controller in the service)?

Thanks in advance.


Willy Denoyette said:
|I am working at .net 1.1, writing in c#.
| I have windows service with a COM object.
| Every unexpected time The COM object throw an error that make my service
get
| stuck (do not respond).
| I can catch this error.
| I want to restart my windows service every time the COM object throws an
| error.
|
| I use System.ServiceProcess.ServiceController to stop and start my
service.
| But there is one thing I do not understand:
| When I write in the service code:
|
|
Code:
| MyController.Stop()
| MyController.Start()
|
|
| How can the service reach the second line if I stop it?
| Is it a good way to start and stop my service or should I use other way?
|
| Thanks in advance!
|

Your real problem won't get solved by restarting your service. You have to
find out what's the cause of the COM error (an exception?).
Main cause of such errors is a COM object that runs in a context for which
it was not designed, like ActiveX controls, or COM objects that are called
from incompatible apartments.
Willy.
 
It would be easier to write a small app (doesnt need a ui) which you can
start from your service with the System.Diagnostics.Process task. That could
then stop and start your service and log things. If you simply stop and start
then write a batch file
containing NET STOP and NET START commands and start that.

Ciaran O'Donnell

Jianwei Sun said:
In this case, I think you need to write another service called
"MyServiceWatcher" which will start the service for you.
Willy,

I use the COM object as it is, because I do not have its code, and I can not
fix it.
The COM Exception I talk about happens ones in a week.
I set the properties of the service to restart on failure so when COM
Exception that make the service fall happens it restarts and works well.

This specific exception does not stop the service and visually (in the event
viewer) it looks like the service works, but it does not.
I know that restart the service after an error like this will solve the
problem, because this Exception is not something permanent (happens once a
week), and after restarting it manually it works well.
That’s why I want to restart it automatically programmatically.

1) Do you think that service controller is a good way?
2) Will the service controller continue work after I stop the service (I
declare the service controller in the service)?

Thanks in advance.


Willy Denoyette said:
|I am working at .net 1.1, writing in c#.
| I have windows service with a COM object.
| Every unexpected time The COM object throw an error that make my service
get
| stuck (do not respond).
| I can catch this error.
| I want to restart my windows service every time the COM object throws an
| error.
|
| I use System.ServiceProcess.ServiceController to stop and start my
service.
| But there is one thing I do not understand:
| When I write in the service code:
|
|
Code:
| MyController.Stop()
| MyController.Start()
|
|
| How can the service reach the second line if I stop it?
| Is it a good way to start and stop my service or should I use other way?
|
| Thanks in advance!
|

Your real problem won't get solved by restarting your service. You have to
find out what's the cause of the COM error (an exception?).
Main cause of such errors is a COM object that runs in a context for which
it was not designed, like ActiveX controls, or COM objects that are called
from incompatible apartments.
Willy.
 
| Willy,
|
| I use the COM object as it is, because I do not have its code, and I can
not
| fix it.

True, like most do, but not having the source doesn't mean you should not
take care your object is designed to run in a service environment, note a
lot of COM servers aren't designed/tested in a service, most if not all
ActiveX objects fall into this category.
..
Another thing you should do, is look at the apartment requirements, if it's
a STA type of object (see oleview.exe) I would suggest you to create an
instance from a STA thread, note that your service thread will run in an
MTA. Not fixing this can lead to dealocks caused by the finalizer failing to
run. The result of this all can be such that the memory consumption of your
service grows by such an extend that you finaly run into OOM exceptions.
This is especially bad for your service but also for the other applications
running.

| The COM Exception I talk about happens ones in a week.
| I set the properties of the service to restart on failure so when COM
| Exception that make the service fall happens it restarts and works well.
|
| This specific exception does not stop the service and visually (in the
event
| viewer) it looks like the service works, but it does not.
| I know that restart the service after an error like this will solve the
| problem, because this Exception is not something permanent (happens once a
| week), and after restarting it manually it works well.
| Thatâ?Ts why I want to restart it automatically programmatically.
|
| 1) Do you think that service controller is a good way?
| 2) Will the service controller continue work after I stop the service (I
| declare the service controller in the service)?
|

Sure you can use the ServiceController but you need to run this from another
program (or watchdog service).


Willy.
 
Back
Top