Roberto Nicchi Posted June 1, 2021 Share Posted June 1, 2021 I have first created my application as standalone project. Now i have created the service application project. The service start and works fine but when i try to stop it, it doesn't respond and can't be stopped. The problem seems related to a thread that is created in the servermodule. If i remove this thread, the service can be stopped with no problem. Probably i'm not doing it correcty. I'm going to describe how it work: In the server module oncreate event i have used a code like this: FmyThread:=Tthread.CreateAnonymousThread(myproc); FmyThread.Start; FmyThread is defined in the private section of the server module. In the server module onbeforeshutdown event i have placed code to terminate the thread FmyThread.Terminate; The myproc procedure is like this procedure TUniServerModule.myproc; var Start,Finish:Tdatetime; const SecBetweenRuns = 60; begin Start:=now; while not Terminated do begin Finish:=now; if SecondsBetween(Start,Finish)>SecBetweenRuns then begin // Do something, the code execution is fast start:=Finish; end; sleep(1000); end; end; Quote Link to comment Share on other sites More sharing options...
Mehmet Emin Posted June 1, 2021 Share Posted June 1, 2021 Since you are using CreateAnonymousThread and calling TUniServerModule.myproc, TUniServerModule has already same named property "Terminated". So I think you would better have your thread class and check the threads "Terminated" property within. Then you be able to exit the while loop. Also after FmyThread.Terminate; you can call thread waitfor better cleanup. Quote Link to comment Share on other sites More sharing options...
Roberto Nicchi Posted June 3, 2021 Author Share Posted June 3, 2021 On 6/1/2021 at 6:46 PM, Mehmet Emin said: Since you are using CreateAnonymousThread and calling TUniServerModule.myproc, TUniServerModule has already same named property "Terminated". So I think you would better have your thread class and check the threads "Terminated" property within. Then you be able to exit the while loop. Also after FmyThread.Terminate; you can call thread waitfor better cleanup. Got it, thanks ! Roberto Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.