Windows service (Restart itself)

  • Thread starter Thread starter iceman
  • Start date Start date
I

iceman

Hello,

I have a windows service. I want to restart it after every 24 hour. Is it
possible to restart the service programmatically(from the service itself)
using the sercvice controller object? Or should i chose some other approach?

Any ideas?

Thanx for all help.
- Iceman
 
If you don't want to get complicated, use the NT SC.EXE (service controller)
in command line mode to start your service. Set the command line for it in
the system scheduler.

-Rob Teixeira [MVP]
 
Although you could user a ServiceController to stop your service, once it is
stopped it won't be able to start itself.

By far the easiest way is to have a batch file with:

net stop <servicename>
net start <servicename>

and schedule that to run once a day at the desired time of day.
 
Been really busy the last few weeks - just finished a major overhaul of a
fairly large project.
 
Could i solve this issue by starting a new process(a bat file which contains
comands to stop and then start the service) from the windows service?
Is it possible?

(I have actually no control over the system schedular.)

Thanx
-Iceman
 
Could i solve this issue by starting a new process(a bat file which contains
comands to stop and then start the service) from the windows service?
Is it possible?

(I have actually no control over the system schedular.)

Thanx
-Iceman
 
Hi Iceman,

Why do you want to do that?
Did you get memory leaks so that you work around in this way?

If it was a COM+ service, I think you use COM+ administration library to
do the stuff.
Automating COM+ Administration
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cossdk/htm/
pgdeployingapplications_5cmf.asp

For a regular service, I think you need to use a watchdog service to do
it.(similar with the task schedule).
But you should rather fix your memory leaks.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Peter,

The services I am talking about is completely developed by managed code. The
reason i awant to restart the services for example each 24 hours is because
"some times" one of those services stops to do the task they was made for.
The service status is still running in the serivce control manager. When the
service is restarted then everything works fine... I am not sure why this
happens.... I have try and catch blocks "everywhere" in the code. Database
connections are Creted / Opened / Closed / Distroyed .etc.

Any ideas?

Thanx for you help and time.

Best regards
-Iceman
 
Hi Iceman,

I think you may try to do the task in a seperate thread in which it will
signal an event to tell the service process that it was alive. If the
signal doesn't produce in a fixed period, there should be something wrong
with the working thread, you may try to Abort the thread and restart it
again.

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Peter,

I am using multiple threads in the windows service. Some thread works fine
and some doesnt. Maybe there is a good idea to abort the thread an restart
it. I am actually confused why some of the threads "die". Is it because it
occurs unexpected errors? Could it be something else? Any ideas?

Thanx
-Iceman
 
Hi Iceman,

Usually it may be caused by waiting for an sychronized object.
Since in multiple thread programming, you must use the sychronized
mechanism to make the threads works properly.

So you may try to check if the "dead" thread is waiting for an synchronized
object, or it attempted to access a resouce which is occupied by another
one thread or process.

Or you are execute an time-consuming procudure(e.g. query in the database)?

Can you tell me what are the "die" threads doing?

You may try to set the timeout value when you wait for a resource, so that
the thread will not wait infinitely.

If you have any concern on this issue,please post here.


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi again Peter,

The dieing threads as actually getting data from an sqlserver 2000 database.
The queries are actaully not very time consuming(sometimes these services
runs on servers which with heavy traffic.)

I havent used the syncronised object before, maybe I can use it somehow to
solve the issue. I will look more around this object....

If I set the timeout property and the thread does throw an exception. Am I
able to restart the exactly the same thread?

(Another information maybe i should have told you this at an earlier point.
The services can run stable more than a month before this kind "dieing"
thread fenomenal occurs. Sometimes more somtimes less and sometimes it
doesent occur at all.)

Thanx for your time and help.

Best regards
-Iceman
 
Hi Iceman,

Thank you for your reply.
If I set the timeout property and the thread does throw an exception. Am I
able to restart the exactly the same thread?

I think this due to how you handle the exception, when you run the thread,
the thread must has change the state of some variables or some other
resource, so you should reset them properly so that when the thread
restart, the thread running environment will be persists. That is to say,
if you abort a thread and restart it the thread will run from the beginning
of the thread proc, so it is important to maintain the original state of
the resource in the thread to get the same behavior.

To troubleshoot the problem, I think you may need to write an log to the
file after every function call in your "dead" thread, so that you can
locate where code line cause the problem. You may have a few tests to see
if the problem persists in the same code line.

Here is an link about multiple thread programming you may take a look.
Threading Tutorial
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vcwlkThreadingTutorial.asp

Based on my experience, the synchronized object is used to control the
multiple threads access one or more resource. e.g. you have an global
variable, if one thread will write it and another one will read it, this
will cause problem when the two threads do the read or write operation in
the mean time.
(Another information maybe i should have told you this at an earlier point.
The services can run stable more than a month before this kind "dieing"
thread fenomenal occurs. Sometimes more somtimes less and sometimes it
doesent occur at all.)

If there exists multiple threads accessing one resource and you did not use
the synchronized mechanism to synchronize the threads, the behavior is very
strange.

If this is a big project, I think you may need to call Microsoft Product
Support Services (PSS) for further help.

If you have any concern on this issue, please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Peter,

First of all I would like to thank you for your help and time. I have read
more about the syncronisation object and it seems like this is what I have
to implement to get "more stability".

Again thank you very much for your help.

Merry X-mas

Best regards
-Iceman
 
Back
Top