Home All Groups Group Topic Archive Search About

Problem restarting a windows service with ServiceController

Author
22 Sep 2006 11:58 AM
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!

Author
22 Sep 2006 1:47 PM
Ciaran O''Donnell
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

Show quoteHide quote
"sergio.call***@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.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!
>
>
Are all your drivers up to date? click for free checkup

Author
22 Sep 2006 2:19 PM
Willy Denoyette [MVP]
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.




<sergio.call***@gmail.com> wrote in message
Show quoteHide quote
news:1158926326.240038.85000@d34g2000cwd.googlegroups.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.ServiceProcess.ServiceControllerStatus.Stopped);
Show quoteHide quote
|
| 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!
|
Author
22 Sep 2006 3:29 PM
Ignacio Machin ( .NET/ C# MVP )
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

<sergio.call***@gmail.com> wrote in message
Show quoteHide quote
news:1158926326.240038.85000@d34g2000cwd.googlegroups.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.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!
>
Author
22 Sep 2006 4:26 PM
Willy Denoyette [MVP]
Show quote Hide quote
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uXzTjvl3GHA.2420@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.
Author
26 Sep 2006 11:28 AM
sergio.calleja
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:

Show quoteHide quote
> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
> in message news:uXzTjvl3GHA.2420@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.
Author
26 Sep 2006 1:55 PM
Ignacio Machin ( .NET/ C# MVP )
Hi,

Show quoteHide quote
"Willy Denoyette [MVP]" <willy.denoye***@telenet.be> wrote in message
news:uqqabPm3GHA.1304@TK2MSFTNGP05.phx.gbl...
>
> "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
> wrote
> in message news:uXzTjvl3GHA.2420@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 )

Bookmark and Share