Hi there.
Send a message from the thread to the main app. Let the thread die or not, this is up to you. The only thing what you should to is to let the main app know wether the thread is dead or not. This could be done by setting the 'FreeOnTerminate' property to false.
Ok, here are my ideas:
Delphi-Quellcode:
Procedure TMyThread.Execute;
Begin
While Not Terminated Do Begin
.... // Most likely you would have something like 'WaitForSingleObject' here to preserve CPU cycles.
If MainAppMustRestart Then Begin
PostMessage(MainForm.Handle, WM_RESTARTAPPLICATION,0,0);
Terminate; // If you want
End
End
End;
....
Type
TMainForm = Class (TForm)
...
Procedure wmRestart (War Msg : TMessage); Message WM_RESTARTAPPLICATION;
End;
...
Procedure TMainForm.wmRestar (Var Msg : TMessage);
Begin
With MyThread Do Begin
Terminate;
WaitFor;
Free;
End;
Application.Terminate;
Close;
End;
Should'nt cause much problems.
In case you have troubles with threads, consider downloading my
workerthread pool and define a 'kill application job', which essentially does the message sending.