From Lina with IT

Wednesday, February 16, 2005

Threading

udah lama ga utak-atik delphi lagi. Mulai liat code-code dari Marco Cantu di Mastering Delphi 7 Code. Dari dulu ingin memperdalam fungsi threading. Threading is the process that allows various transactions to be executed concurrently.

Threading source from Mastering Delphi 7-Marco Cantu

// custom thread class

type
TPrimeAdder = class(TThread)
private
FMax, FTotal, FPosition: Integer;
protected
procedure Execute; override;
procedure ShowTotal;
procedure UpdateProgress;
public
property Max: Integer read FMax write FMax;
end;

procedure TPrimeAdder.Execute;
var
I, Tot: Integer;
begin
Tot := 0;
for I := 1 to FMax do
begin
if IsPrime (I) then
Tot := Tot + I;
if I mod (fMax div 100) = 0 then
begin
FPosition := I * 100 div fMax;
Synchronize(UpdateProgress);
end;
end;
FTotal := Tot;
Synchronize(ShowTotal);
end;

procedure TPrimeAdder.ShowTotal;
begin
ShowMessage ('Thread: ' + IntToStr (FTotal));
end;

procedure TPrimeAdder.UpdateProgress;
begin
Form1.ProgressBar1.Position := fPosition;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
AdderThread: TPrimeAdder;
begin
AdderThread := TPrimeAdder.Create (True);
AdderThread.Max := Max;
AdderThread.FreeOnTerminate := True;
AdderThread.Resume;
end;

0 Comments:

Post a Comment

<< Home