|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem restarting a windows service with ServiceControlleri 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 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! > > 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... serviceController1.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);| 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"); | 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! | 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. -- Show quoteHide quote-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation <sergio.call***@gmail.com> wrote in message 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! >
Show quote
Hide quote
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote You don't do the same thing as the OP I guess, you can't stop or start your 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. | | 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: 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. Hi,
Show quoteHide quote "Willy Denoyette [MVP]" <willy.denoye***@telenet.be> wrote in message Hi,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? 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 )
declare array with a default value?
Binding bug regarding visible property. Relection on interfaces Writing update back to MSAccess table? Closing IDbConnection Problems when writing a SMTP client and talk with the hotmail Implementing Nested Transaction in C# Looking for good referencing on unmanaged code interop Excel SaveAs problem Forms |
|||||||||||||||||||||||