Einzelnen Beitrag anzeigen

alzaimar
(Moderator)

Registriert seit: 6. Mai 2005
Ort: Berlin
4.956 Beiträge
 
Delphi 2007 Enterprise
 
#7

Re: Restarting Main Application in TThread Class

  Alt 7. Feb 2008, 21:24
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.
"Wenn ist das Nunstruck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput!"
(Monty Python "Joke Warefare")
  Mit Zitat antworten Zitat