Windows service is a more practical function. Your program can start to run when windows is not logged on. It is not affected by user logout and cannot be closed by users by mistake. However, writing services may not be easy. Fortunately, Delphi provides us with a template to easily compile a standard Windows service program. First, create a service application in Delphi. At this point, Delphi has built a Service Program Framework for us. We just need to add our code to the appropriate position. Generally, a thread is required in the service to work continuously. The timer may also work, but the thread works better. Delphi will generate a visualized service container, and you can have some necessary controls on it, but because it is a service program, there is no interface display, therefore, it is not recommended to place controls such as edit on it. The Service should only handle the work, and the display interface should be completed by other programs. The displayname attribute of the service control is the name displayed on the left of the management tools-> service, while the name attribute is the service name. When you start or stop a service from a command prompt, it is needed. In the event onstart, we should finish the thread startup. For example: Procedure ts2hconv. servicestart (Sender: tservice; VaR started: Boolean ); VaR REG: Tregistry; Logfilename, logpath: string; Sltemp: tstringlist; Begin Coinitialize (NiL ); Reg: = Tregistry. Create; Reg. rootkey: = HKEY_LOCAL_MACHINE; Reg. openkey ('/software/bhome/Education', true ); Logpath: = trim (Reg. readstring ('logpath ')); Sourceconnstr: = trim (Reg. readstring ('sourceconnstr ')); If trim (logpath) = ''then Logpath: = 'C :/'; Reg. closekey; Reg. Free; If rightstr (logpath, 1) <> '/' then Logpath: = logpath + '/'; Logfilename: Invalid logpath1_formatdatetime('yyyymmdd', now1_'log.txt '; Try If not fileexists (logfilename) then begin Sltemp: = tstringlist. Create; Sltemp. Clear; Sltemp. savetofile (logfilename ); Sltemp. Free; End; Assignfile (logfile, logfilename ); Append (logfile ); Except Started: = false; Exit; End; Started: = true; Try Ac_source: = tadoconnection. Create (NiL ); Q_source: = tadoquery. Create (NiL ); Q_source.connection: = ac_source; Try Ac_source.close; Ac_source.connectionstring: = sourceconnstr; Ac_source.open; Syslog ('connection to source database successful! '); Dbok: = true; Except On E: exception do begin Dbok: = false; Syslog ('database connection failed! '+ E. Message ); End; End; Myphsthread: = tphssendthread. Create (); Myphsthread. freeonterminate: = true; Myphsthread. Priority: = tplower; End; In the onstop event, we should stop the thread and release open resources. Note that when you stop the thread, the terminate method is generally used, use the terminated attribute in the thread to determine whether to end the thread. Because it is a thread and the main process is running at a time, it is possible that you are just performing terminate, the thread has just been terminated and is processing time-consuming resources. When the main process immediately executes terminate and releases resources, it will cause thread execution errors, therefore, you must wait until the thread stops correctly before the main process can release resources. For example: Procedure ts2hconv. servicestop (Sender: tservice; VaR stopped: Boolean ); Begin Try Myphsthread. Terminate; While _ threadisrun do Sleep (1000 ); Ac_source.close; Closefile (logfile ); Freeandnil (q_source ); Freeandnil (ac_source ); Couninitialize; Except End; Stopped: = true; End; |