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

Problem restarting a windows service with ServiceController

P: n/a
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.ServicePro cess.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!

Sep 22 '06 #1
Share this Question
Share on Google+
6 Replies


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

"se************@gmail.com" wrote:
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.ServicePro cess.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!

Sep 22 '06 #2

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


<se************@gmail.comwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
| 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.ServicePro cess.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!
|
Sep 22 '06 #3

P: n/a
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,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<se************@gmail.comwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
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.ServicePro cess.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!

Sep 22 '06 #4

P: n/a

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.uswrote
in message news:uX**************@TK2MSFTNGP02.phx.gbl...
| 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.


Sep 22 '06 #5

P: n/a
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:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.uswrote
in message news:uX**************@TK2MSFTNGP02.phx.gbl...
| 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.
Sep 26 '06 #6

P: n/a
Hi,

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uq**************@TK2MSFTNGP05.phx.gbl...
>
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:uX**************@TK2MSFTNGP02.phx.gbl...
| 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 )
Sep 26 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.