473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7197
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Niel | last post by:
Hello friends, I think this issue has been posted before but i have not been able to get any solution that is why i am posting it again in detail as to what exactly is happneing in my case. Hope...
5
by: Paul O. Morris | last post by:
Is there a script that I can run to stop a particular SQL server service on Win2003 server? I'm looking for a similar script to restart that service as well. Thanks.
2
by: 0to60 | last post by:
I have a windows service that when started creates two threads: one thread that runs a TcpListener waiting for TcpClients to connect, and another thread that reads from the resulting sockets. My...
1
by: UJ | last post by:
I have code to start up a service and delete (uninstall) a service. I'm looking for help on stopping a service. I've noticed that if a service is running and you delete it, it still is running but...
4
by: vickaiser1 | last post by:
I am creating a setup project for an application and need to edit the Metabase.xml file during setup. Does anyone know of a way to stop and then restart services programatically before and after I...
2
by: Nathan | last post by:
I have created a windows service but am having difficulties handling exceptions in the OnStart. What I want to do is if an exeception occurs during any of the code executed during the OnStart is...
2
by: MA | last post by:
Hi all! I have developed a webservice that writeing and reading files in different folders. I need to be able to start and stop this service by using a web interface (start and stop button)....
2
by: Chris Dunaway | last post by:
I need to be able to start and stop a service on another computer on the network. But I cannot connect to the other computer using my standard user credentials. I have a separate login for that...
1
by: aj | last post by:
A few service stop/start/restart questions on SQL Server 2005 SP2, which I'll call SQLS. It looks as if there are *potentially* 6 ways to start/stop SQLS Services like the engine itself,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.