TThread.Create and TThread.Synchronize example:

Under UNIX compile with -dUseCThreads.

interface

[...]

  { TMyThread }

  TMyThread=class(TThread)
    procedure Execute; override;
    procedure UpdateGUI;
  end;

var
  Form1: TForm1;
  MyThread1:TMyThread;
  i:integer;

implementation

{ TMyThread }

procedure TMyThread.Execute;
begin
  repeat
    inc(i);
    Synchronize(@UpdateGUI);
  until Terminated;
end;

procedure TMyThread.UpdateGUI;
begin
  Form1.Edit1.Text:=IntToStr(i);
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  i:=0;
  if not assigned(MyThread1) then
    MyThread1:=TMyThread.Create(false); // Starts thread
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  FreeAndNil(MyThread1);  // Terminates thread
end;

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to