Jump to content

TThread in UNIGUI service. The service can't be stopped.


Roberto Nicchi

Recommended Posts

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...