Note that there are some explanatory texts on larger screens.

plurals
  1. POhow do I get windows SCM to restart my service when it fails
    primarykey
    data
    text
    <p>I have some windows services that i have written in delphi and they generally work very well, however on occasion i do get an exception thrown that can be considered fatal. When this happens the service is designed to stop. </p> <p>My question is how do i exit the service in such a way that the SCM will automatically try to restart the service. (I have already set the recovery options for the service in the service manager)</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms685939%28VS.85%29.aspx" rel="noreferrer">MSDN</a> states </p> <blockquote> <p>A service is considered failed when it terminates without reporting a status of SERVICE_STOPPED to the service controller.</p> </blockquote> <p>i have read this blog post <a href="http://mdenomy.wordpress.com/2008/02/28/using-the-automatic-recovery-features-of-windows-services/" rel="noreferrer">Using the Automatic Recovery Features of Windows Services</a> but i am not sure how to implement this in delphi.</p> <p>i have allready tried the following</p> <ul> <li>Setting the ErrCode Property of the TService to a non zero value.</li> <li>Setting the stopped Parameter of the ServiceStop Event to false.</li> <li>Raising an exception in the servicestop event handler.</li> </ul> <p><strong>EDIT 2013-08-06 added code example</strong> </p> <p><em><strong>Code Now Updated to show working example</em></strong></p> <p>Here is the code im using, </p> <pre><code>procedure TTestService.ServiceExecute(Sender: TService); begin while not (Terminated or FFatalError) do begin ServiceThread.ProcessRequests(False); ReportStatus; Sleep(100); end; if FFatalError then Halt(1); end; </code></pre> <p>FFatalError is a private boolean field on the TTestService class and is initialized to false on startup, it is only set to true if the worker thread (started in the TTestService.ServiceStart event) terminates with a fatal exception. </p> <p>here is the OnTerminate event handler for the worker thread.</p> <pre><code>procedure TTestService.ThdTerm(Sender: Tobject); var E : Exception; Thread : TThread; begin Thread := TThread(Sender); if (Thread.FatalException &lt;&gt; nil) then begin E := Exception(Thread.FatalException); GetExcCallStack(E); EventLog.LogError(Thread.ClassName + ': ID:'+ IntToStr(Thread.ThreadID) + ' Stopped Unexpectedly!, '+ NEWLINE + E.ClassName +': ' + E.Message); FFatalError := True; end; end; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload