Einzelnen Beitrag anzeigen

alzaimar
(Moderator)

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

Re: Basic TThread doesn't work

  Alt 1. Feb 2008, 09:12
This is a bit tricky. Here's a simple example:
Delphi-Quellcode:
Type
  TMythread = Class (TThread)
  private
     fLabelMsg : String; // a private field to display in the main form from within a thread
     procedure DoShowMessage;
  Protected
     Procedure Execute; Override;
  End;
Procedure TMyThread.DoShowMessage;
Begin
  MainForm.MyLabel.Caption := fLabelMsg;
End;

Procedure TMyThread.Execute;
Begin
  ....
  fLabelMsg :='Show this!'; // Write what you want to display in your private fields
  Synchronize (DoShowMessage); // Call the method which actually talks to the VCL via Synchronize
  ...
// The Thread terminates automatically, when the Execute-Method is exited.
End;
Bear in mind that ANY access to VCL-Controls (not only on the main form) must be wrapped in a Synchronize call, otherwise your app will hang.
"Wenn ist das Nunstruck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput!"
(Monty Python "Joke Warefare")
  Mit Zitat antworten Zitat