By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,208 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,208 IT Pros & Developers. It's quick & easy.

Stop and start windows service programatically

P: n/a
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:

Expand|Select|Wrap|Line Numbers
  1. MyController.Stop()
  2. MyController.Start()
  3.  
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!

Sep 11 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
shai wrote:
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:

Expand|Select|Wrap|Line Numbers
  1. MyController.Stop()
  2. MyController.Start()
  3.  

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!
Sep 11 '06 #2

P: n/a
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" wrote:
shai wrote:
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:

Expand|Select|Wrap|Line Numbers
  1.  MyController.Stop()
  2.  MyController.Start()
  3.  
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!
Sep 11 '06 #3

P: n/a

"shai" <sh**@discussions.microsoft.comwrote in message
news:44**********************************@microsof t.com...
|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:
|
|
Expand|Select|Wrap|Line Numbers
  1. | MyController.Stop()
  2. | 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.
Sep 11 '06 #4

P: n/a
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 [MVP]" wrote:
>
"shai" <sh**@discussions.microsoft.comwrote in message
news:44**********************************@microsof t.com...
|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:
|
|
Expand|Select|Wrap|Line Numbers
  1. | MyController.Stop()
  2. | 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.
Sep 11 '06 #5

P: n/a
In this case, I think you need to write another service called
"MyServiceWatcher" which will start the service for you.

shai wrote:
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 [MVP]" wrote:
>"shai" <sh**@discussions.microsoft.comwrote in message
news:44**********************************@microso ft.com...
|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:
|
|
Expand|Select|Wrap|Line Numbers
  1. | MyController.Stop()
  2. | 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.
Sep 11 '06 #6

P: n/a
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" wrote:
In this case, I think you need to write another service called
"MyServiceWatcher" which will start the service for you.

shai wrote:
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 [MVP]" wrote:
"shai" <sh**@discussions.microsoft.comwrote in message
news:44**********************************@microsof t.com...
|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:
|
|
Expand|Select|Wrap|Line Numbers
  1. | MyController.Stop()
  2. | 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.
Sep 11 '06 #7

P: n/a

"shai" <sh**@discussions.microsoft.comwrote in message
news:10**********************************@microsof t.com...
| 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.
Sep 11 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.