unit PrimeThread;
 
interface
 
uses
 Classes;
 
type
 TPrimeThrd = class(TThread)
 private
 FTestNumber: integer;
 FResultString: string;
 protected
 function IsPrime: boolean;
 procedure UpdateResults;
 procedure Execute; override;
 public
 property TestNumber: integer write FTestNumber;
 end;
 
implementation
 
uses SysUtils, Dialogs, PrimeForm, Windows;
 
procedure TPrimeThrd.UpdateResults;
begin
 PrimeFrm.ResultsMemo.Lines.Add(FResultString);
end;
 
function TPrimeThrd.IsPrime: boolean;
 
var
 iter: integer;
 
begin
 result := true;
 if FTestNumber < 0 then
 begin
 result := false;
 exit;
 end;
 if FTestNumber <= 2 then
 exit;
 iter := 2;
 while (iter < FTestNumber) and (not terminated) do {Line A}
 begin
 if (FTestNumber mod iter) = 0 then
 begin
 result := false;
 {exit;}
 end;
 Inc(iter);
 end;
end;
 
procedure TPrimeThrd.Execute;
begin
 if IsPrime then
 FResultString := IntToStr(FTestNumber) + ' es primo.'
 else
 FResultString := IntToStr(FTestNumber) + ' no es primo.';
 if not Terminated then {Linea B}
 begin
 Synchronize(UpdateResults);
 PostMessage(PrimeFrm.Handle, WM_THREAD_COMPLETE, 0, 0);
 end;
end;
 
end.