Problem restarting a windows service with ServiceController

  • Thread starter Thread starter sergio.calleja
  • Start date Start date
S

sergio.calleja

Hi Everybody,
i need to restart a windows service made with c# when an fixed event is
raised.
So to test it, I've added a servicecontroller to my service, and in the
creation method, i've created a new thread to try to stop it.
This is my code:

public Service1()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
log = new EventLog("Application", ".", "ServiceTest");
_timer = new Timer(new TimerCallback(OnTimer), null, 20000,0);
}

private void OnTimer(object state)
{
try
{
log.WriteEntry("Timer");
serviceController1.Stop();
log.WriteEntry("Stopping");
serviceController1.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);

log.WriteEntry("starting");
serviceController1.Start();
log.WriteEntry("Restarted");
}
catch (Exception ex)
{
log.WriteEntry(ex.ToString());
}

}

When service is stopped it doesn't start again, what's the problem? Can
you help me?

Thanks!
 
The issue is when you service stops, it is stopped. The code is stopped and
therefore never calls the start command. You need to call some other program
to stop and resart the service.

Ciaran O'Donnell
 
You can't control your service from within the same service.
You need another program to stop and start services.
I'm not clear on why you need to restart a service anyway, services are
invented to run from boot to shutdown time, and should only restart when a
catastrofic failure prevents the service to continue it's tasks. In that
case the service can be set-up to restart under control of the SCM.

Willy.




| Hi Everybody,
| i need to restart a windows service made with c# when an fixed event is
| raised.
| So to test it, I've added a servicecontroller to my service, and in the
| creation method, i've created a new thread to try to stop it.
| This is my code:
|
| public Service1()
| {
| // This call is required by the Windows.Forms Component Designer.
| InitializeComponent();
| log = new EventLog("Application", ".", "ServiceTest");
| _timer = new Timer(new TimerCallback(OnTimer), null, 20000,0);
| }
|
| private void OnTimer(object state)
| {
| try
| {
| log.WriteEntry("Timer");
| serviceController1.Stop();
| log.WriteEntry("Stopping");
|
serviceController1.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);
|
| log.WriteEntry("starting");
| serviceController1.Start();
| log.WriteEntry("Restarted");
| }
| catch (Exception ex)
| {
| log.WriteEntry(ex.ToString());
| }
|
| }
|
| When service is stopped it doesn't start again, what's the problem? Can
| you help me?
|
| Thanks!
|
 
Hi,

Do you get any error?

Does the service do logging?

I have a similar situation, I have a service that create a TCP Listener in a
worker thread of course, if you stop and restart it right away the worker
thread (or at least the port ) is still active and you get error.
 
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message | Hi,
|
| Do you get any error?
|
| Does the service do logging?
|
| I have a similar situation, I have a service that create a TCP Listener in
a
| worker thread of course, if you stop and restart it right away the worker
| thread (or at least the port ) is still active and you get error.
|
|

You don't do the same thing as the OP I guess, you can't stop or start your
own service using the service controller. How would you think
serviceController1.Start(); could ever be reached?

Willy.
 
Thank you everybody,
Last day at late hour i get the same conclusion :( , a service cannot
be restarted himself.


Thx

Willy Denoyette [MVP] ha escrito:
 
Hi,

Willy Denoyette said:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message | Hi,
|
| Do you get any error?
|
| Does the service do logging?
|
| I have a similar situation, I have a service that create a TCP Listener
in
a
| worker thread of course, if you stop and restart it right away the
worker
| thread (or at least the port ) is still active and you get error.
|
|

You don't do the same thing as the OP I guess, you can't stop or start
your
own service using the service controller. How would you think
serviceController1.Start(); could ever be reached?

Hi,

Not at all, I did not read the OP correctly, I assumed he meant from another
app, (in my case it's the user manually )
 
Back
Top