AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi Restarting Main Application in TThread Class
Thema durchsuchen
Ansicht
Themen-Optionen

Restarting Main Application in TThread Class

Ein Thema von FaNIX · begonnen am 1. Feb 2008 · letzter Beitrag vom 7. Feb 2008
Antwort Antwort
FaNIX

Registriert seit: 8. Okt 2007
36 Beiträge
 
#1

Restarting Main Application in TThread Class

  Alt 1. Feb 2008, 13:00
Hi People,

I am trying to restart my Main Application by using code inside a Thread. Basicly my restart function works 100% when placed inside the Main Application code. But since a TThread runs in another memory space, i think reference to the Application.Terminate is not set to the Main Application, maybe to the thread, I don't know.

Here is my restart procedure inside my Thread:

Delphi-Quellcode:
procedure TUpdateLibThread.AppRestart;
var
  HANDLE : HWND;
begin
  ShellExecute(HANDLE,'open',Application.ExeName, '', '', 1) ;
  Application.Terminate;
end;
The thread is called by die main application, but the restart application must be inside the thread, as the outcome of the thread determines if the application should be restarted or not. If there is a way to return a value back to the main application when the thread ends, then i might be able to use that to determine if i need to call the restart procedure...

Help?
  Mit Zitat antworten Zitat
Muetze1
(Gast)

n/a Beiträge
 
#2

Re: Restarting Main Application in TThread Class

  Alt 1. Feb 2008, 16:09
I do not understand how this should work, because the TThread descendant belongs to your process and if application is terminated, then the process ends (and there also the thread belonging to this process, so also your thread).

And your thread runs in the same memory space as your process (better: your main VCL thread). So you can access that memory, but you have to synchronize reentrant acces due these two threads. And your statement about the application instance is not correct: on start of your process (VCL App), the application instance is created and can be referenced by the global application variable. This application instance then splits the VCL thread containing the message loop. Your thread also runs in this process and the synchronize method of TThread instances just sending a message to the message loop of VCL main thread containing an address to call from the VCL thread. Thats all about synchronizing. So, since the application variable is global, there is only one instance and so its the same as your thread can access.
  Mit Zitat antworten Zitat
FaNIX

Registriert seit: 8. Okt 2007
36 Beiträge
 
#3

Re: Restarting Main Application in TThread Class

  Alt 1. Feb 2008, 18:29
Zitat von Muetze1:
I do not understand how this should work, because the TThread descendant belongs to your process and if application is terminated, then the process ends (and there also the thread belonging to this process, so also your thread).

And your thread runs in the same memory space as your process (better: your main VCL thread). So you can access that memory, but you have to synchronize reentrant acces due these two threads. And your statement about the application instance is not correct: on start of your process (VCL App), the application instance is created and can be referenced by the global application variable. This application instance then splits the VCL thread containing the message loop. Your thread also runs in this process and the synchronize method of TThread instances just sending a message to the message loop of VCL main thread containing an address to call from the VCL thread. Thats all about synchronizing. So, since the application variable is global, there is only one instance and so its the same as your thread can access.
Thank your for your feedback, back some code will help more
  Mit Zitat antworten Zitat
FaNIX

Registriert seit: 8. Okt 2007
36 Beiträge
 
#4

Re: Restarting Main Application in TThread Class

  Alt 4. Feb 2008, 07:33
  Mit Zitat antworten Zitat
alzaimar
(Moderator)

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

Re: Restarting Main Application in TThread Class

  Alt 4. Feb 2008, 07:56
Why don't you send a message to the main form which then terminates your thread, waits for it and finally restarts the application?
"Wenn ist das Nunstruck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput!"
(Monty Python "Joke Warefare")
  Mit Zitat antworten Zitat
FaNIX

Registriert seit: 8. Okt 2007
36 Beiträge
 
#6

Re: Restarting Main Application in TThread Class

  Alt 4. Feb 2008, 13:43
Zitat von alzaimar:
Why don't you send a message to the main form which then terminates your thread, waits for it and finally restarts the application?
Yes I thought of that, but I'm not sure how to send a message to my Main application once the thread as terminated.

As i said, the Main application must terminate, depending on the outcome of the thread. So the thread should first finish before the outcome is known, and only then can the Main application be terminated.
  Mit Zitat antworten Zitat
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, 22: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
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:52 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz