Board index » delphi » Help with TThread: What am I doing wrong?

Help with TThread: What am I doing wrong?

The following program counts forward to 10000 and writes the numbers
to the main form's canvas. It also creates a TThread object that counts
backward from 10000 and writes the numbers to the main form's caption,
using Synchronize to avoid problems with the VCL. It doesn't work:
The main thread runs to the end and then the secondary thread runs.
If I DONT use Synchronize, the two threads run in parallel as they should.
What the heck am I doing wrong?

Thanks in advance for any advice

Mort Canty, KFA Juelich

-----------------------------------------------------------------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons;

type
  TTstThread = class(TThread)
    j: integer;
   public
    procedure output;  
    procedure execute; override;
  end;
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    i,j: integer;
    aThread: TTstThread;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TTstThread.execute;
begin
   j:= 0;
   while j<10000 do begin
      inc(j);
      synchronize(output)
   end
end;

procedure TTstThread.output;
begin
   form1.canvas.textout(50,1,intToStr(j))
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   with TTstThread.create(false) do priority:= tpNormal;
   i:= 10000;
   while i>0 do begin
      i:= i-1;
      canvas.TextOut(1,1,intTostr(i));
   end
end;

end.

------------------------------------------------------------------------------

 

Re:Help with TThread: What am I doing wrong?


Quote
m.ca...@kfa-juelich.de (Mort Canty) wrote:
>The following program counts forward to 10000 and writes the numbers
>to the main form's canvas. It also creates a TThread object that counts
>backward from 10000 and writes the numbers to the main form's caption,
>using Synchronize to avoid problems with the VCL. It doesn't work:
>The main thread runs to the end and then the secondary thread runs.
>If I DONT use Synchronize, the two threads run in parallel as they should.
>What the heck am I doing wrong?
>Thanks in advance for any advice
>Mort Canty, KFA Juelich
>-----------------------------------------------------------------------------
>unit Unit1;
>interface
>uses
>  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
>  StdCtrls, Buttons;
>type
>  TTstThread = class(TThread)
>    j: integer;
>   public
>    procedure output;  
>    procedure execute; override;
>  end;
>  TForm1 = class(TForm)
>    Button1: TButton;
>    procedure Button1Click(Sender: TObject);
>  private
>    { Private declarations }
>    i,j: integer;
>    aThread: TTstThread;
>  public
>    { Public declarations }
>  end;
>var
>  Form1: TForm1;
>implementation
>{$R *.DFM}
>procedure TTstThread.execute;
>begin
>   j:= 0;
>   while j<10000 do begin
>      inc(j);
>      synchronize(output)
>   end
>end;
>procedure TTstThread.output;
>begin
>   form1.canvas.textout(50,1,intToStr(j))
>end;
>procedure TForm1.Button1Click(Sender: TObject);
>begin
>   with TTstThread.create(false) do priority:= tpNormal;
>   i:= 10000;
>   while i>0 do begin
>      i:= i-1;
>      canvas.TextOut(1,1,intTostr(i));

       Application.ProcessMessages;

Quote
>   end
>end;
>end.
>------------------------------------------------------------------------------

Synchronize calls Output method via sending message into main thread,
and until you not processes messages Output not calls.

Other Threads