Difference between revisions of "Talk:Multithreaded Application Tutorial"

From Free Pascal wiki
(New page: procedure TMyThread.Execute; var newStatus : string; begin fStatusText := 'TMyThread Starting...'; Synchronize(@Showstatus); fStatusText := 'TMyThread Running...'; thi...)
 
Line 24: Line 24:
  
 
and you will see what i mean..
 
and you will see what i mean..
 +
 +
 +
second bad thing:
 +
 +
  procedure TMyThread.ShowStatus;
 +
  // this method is executed by the mainthread and can therefore access all GUI elements.
 +
  begin
 +
    if Assigned(FOnShowStatus) then
 +
    begin
 +
      FOnShowStatus(fStatusText);
 +
    end;
 +
  end;
 +
 +
 +
as you can read: it is es executed by mainthread!!
 +
 +
you are accessing fStatusText from the mainthread and the thread (in worst case, at the same time, )
 +
 +
so using a critical section, would be good..

Revision as of 16:07, 25 January 2010

procedure TMyThread.Execute;

 var
   newStatus : string;
 begin
   fStatusText := 'TMyThread Starting...';
   Synchronize(@Showstatus);
   fStatusText := 'TMyThread Running...';

this is a very good example of what you should NEVER do..

Showstatus will NEVER display the text 'TMyThread Starting...';

will show "TMyThread Running..." maybe twice ...

change your main-thread code to:

 procedure TForm1.ShowStatus(Status: string);
 begin
   memo1.Lines.Add(Status);
 end;


and you will see what i mean..


second bad thing:

 procedure TMyThread.ShowStatus;
 // this method is executed by the mainthread and can therefore access all GUI elements.
 begin
   if Assigned(FOnShowStatus) then
   begin
     FOnShowStatus(fStatusText);
   end;
 end;


as you can read: it is es executed by mainthread!!

you are accessing fStatusText from the mainthread and the thread (in worst case, at the same time, )

so using a critical section, would be good..