OnExecute service error, spawned thread not running

First start your own service in Delphi 7. Follow the documents and start the service, create a custom thread that beeps and logs. Only this is not so. The last attempt was to deliver the same signal and enter the code in the OnExecute event procedure, but when I start the service I get a Windows dialog box saying that it was started and then stopped again.

There must be something obvious that I missed in this code .

Could you take a look? I will also agree with links to simple, working, downloadable sample application projects ... so that I get what gets called every 10 seconds or so, and I'll take it from there.

+1
source share
4 answers

For more information about creating a service, see http://www.delphi3000.com/articles/article_3379.asp . I did this a few years ago, but should still work.

+1
source

Below is an application for servicing bare bones.

Please note that if you want to install the service on Windows Vista and higher using ServiceApp.exe / installation, you must make sure that you run the application with administrator rights.

Also note that despite fmShareDenyWrite, the contents of the log file may not be displayed while the service is running. At least I could not open the file using Notepad ++ until I stopped the service. This may be due to the fact that I had a service running under a system account (unlike my user account).

One more note: If you want your service to be paused and continued, do not use pause and resume. They are not thread safe and are deprecated in D2010 +. Using the T (Simple) event or something similar to control the execution of the main workflow. If you do not want your service to be suspended and continued, you can simply set AllowPause to False.

unit ServiceApp_fm; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs; type TService1 = class(TService) procedure ServiceStart(Sender: TService; var Started: Boolean); procedure ServiceStop(Sender: TService; var Stopped: Boolean); private FWorker: TThread; public function GetServiceController: TServiceController; override; end; var Service1: TService1; implementation {$R *.DFM} type TMainWorkThread = class(TThread) private {$IFDEF UNICODE} FLog: TStreamWriter; {$ELSE} FLog: TFileStream; {$ENDIF} FRepetition: Cardinal; public constructor Create; destructor Destroy; override; procedure Execute; override; end; procedure ServiceController(CtrlCode: DWord); stdcall; begin Service1.Controller(CtrlCode); end; function TService1.GetServiceController: TServiceController; begin Result := ServiceController; end; procedure TService1.ServiceStart(Sender: TService; var Started: Boolean); begin FWorker := TMainWorkThread.Create; Started := True; end; procedure TService1.ServiceStop(Sender: TService; var Stopped: Boolean); begin // Thread should be freed as well as terminated so we don't have a memory // leak. Use FreeAndNil so we can also recognize when the thread isn't // available. (When the service has been stopped but the process hasn't ended // yet or may not even end when the service is restarted instead of "just" stopped. if FWorker <> nil then begin FWorker.Terminate; while WaitForSingleObject(FWorker.Handle, WaitHint-100) = WAIT_TIMEOUT do ReportStatus; FreeAndNil(FWorker); end; Stopped := True; end; { TMainWorkThread } constructor TMainWorkThread.Create; var FileName: String; begin inherited Create({CreateSuspended=}False); FileName := ExtractFilePath(ParamStr(0)) + '\WorkerLog.txt'; {$IFDEF UNICODE} FLog := TStreamWriter.Create(FileName, False, TEncoding.Unicode); {$ELSE} FLog := TFileStream.Create(FileName, fmCreate); {$ENDIF} end; destructor TMainWorkThread.Destroy; begin FLog.Free; inherited; end; procedure TMainWorkThread.Execute; var Text: string; begin inherited; while not Terminated do begin Inc(FRepetition); Text := Format('Logging repetition %d'#13#10, [FRepetition]); {$IFDEF UNICODE} FLog.Write(Text); {$ELSE} FLog.Write(Text[1], Length(Text)); {$ENDIF} Sleep(1000); end; end; end. 
+2
source

The sound signal will not work, see this post .

Your LG procedure is not reliable; it may fail if the log file does not exist. Also, the user of the service must have access to the file. In the first step, you can start the service with a user account for testing.

0
source

Delete below method events

 procedure TAviaABSwedenAMailer.ServiceExecute(Sender: TService); begin while not Terminated do begin Beep; Sleep(500); LG('Amailer is running'); ServiceThread.ProcessRequests(False); end; end; 
0
source

All Articles