469,904 Members | 2,557 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

Problem restarting a windows service with ServiceController

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
6 9078
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
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
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

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

Similar topics

8 posts views Thread by Jerry Camel | last post: by
3 posts views Thread by Doug Bailey | last post: by
4 posts views Thread by tshad | last post: by
4 posts views Thread by =?Utf-8?B?TWFyaw==?= | last post: by
3 posts views Thread by Mika M | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.