472,125 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 software developers and data experts.

Stop and start windows service programatically

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
7 7023
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
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

"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
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
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
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

"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.

Similar topics

5 posts views Thread by Paul O. Morris | last post: by
1 post views Thread by UJ | last post: by
4 posts views Thread by vickaiser1 | last post: by
2 posts views Thread by Nathan | last post: by
2 posts views Thread by MA | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.