Delphi TThread descendant result

SITUATION . I created a unit with some classes for solving algebra (congruents and systems), I show you the code:

type
 TCongrError = class(Exception)
 end;

type
 TCongruence = class(TComponent)
  //code stuff
  constructor Create(a, b, n: integer); virtual;
 end;

type
 TCongrSystem = array of TCongruence;

type
 TCongruenceSystem = class(TThread)
  private
   resInner: integer;
   FData: TCongrSystem;
   function modinv(u, v: integer): integer; //not relevant
  protected
   procedure Execute; override;
  public
   constructor Create(data: TCongrSystem; var result: integer; hasClass: boolean);
 end;

I decided to use TThreadit because this class has an Execute method, which may take some time to finish due to the length of the parameters passed to the constructor. Here's the implementation:

constructor TCongruenceSystem.Create(data: TCongrSystem; var result: integer; hasClass: boolean);
begin

 inherited Create(True);
 FreeOnTerminate := true;

 FData := data;
 setClass := hasClass;
 resInner := result;

end;

procedure TCongruenceSystem.Execute;
var sysResult, i, n, t: integer;
begin

 sysResult := 0;
 n := 1;

 //computation

 Queue( procedure
        begin
         ShowMessage('r = ' + sysResult.ToString);
         resInner := sysResult;
        end );

end;

PROBLEM

If you look at Queue, you will see that I am using (just like a test) ShowMessage and it shows the correct value sysResult. In the second line, by the way, there are some problems that I can not understand.

var result: integer, , resInner := result;. ( ) resInner sysResult, , result - var. ?

, :

constructor TCongruenceSystem.Create(data: TCongrSystem; result: TMemo; hasClass: boolean);
//now of course I have resInner: TMemo

:

Queue( procedure
        begin
         ShowMessage('r = ' + sysResult.ToString);
         resInner.Lines.Add(sysResult.ToString);
        end ); //this code now works properly in both cases! (showmessage and memo)

TMemo, , var result: integer ? ?

, - :

 //I put var a: integer; inside the public part of the TForm
 test := TCongruenceSystem.Create(..., a, true);
 test.OnTerminate := giveMeSolution;
 test.Start;
 test := nil;

giveMeSolution - , a, . , ? Execute , .

ReturnValue, , .

+4
2

Execute - , .

ReturnValue, , .

ReturnValue :

type
  TCongruenceSystem = class(TThread)
    ... 
  protected
    procedure Execute; override;
  public
    property ReturnValue; // protected by default
  end;

procedure TCongruenceSystem.Execute;
var
 ...
begin
  // computation
  ReturnValue := ...;
end;

test := TCongruenceSystem.Create(...);
test.OnTerminate := giveMeSolution;
test.Start;

....

procedure TMyForm.giveMeSolution(Sender: TObject);
var
  Result: Integer;
begin
  Result := TCongruenceSystem(Sender).ReturnValue;
  ...
end;
+3

, FFoo : integer;;

 procedure TFoo.Foo(var x : integer);
 begin
   FFoo := x;
 end;

, , x FFoo. Foo , x, integers - , . integer, FFoo ( resInner) PInteger ( ). ():

TCongruenceSystem = class(TThread)
  private
    resInner: PInteger;       
  protected
    procedure Execute; override;
  public
    constructor Create(result: PInteger);
end;

constructor TCongruenceSystem.Create(result: PInteger);
begin    
  inherited Create(True);
  FreeOnTerminate := true;      
  resInner := result;    
end;

test := TCongruenceSystem.Create(@a); :

 { ** See the bottom of this answer for why NOT to use }
 {    Queue with FreeOnTerminate = true **             }
 Queue( procedure
    begin
      ShowMessage('r = ' + sysResult.ToString);
      resInner^ := sysResult;
    end );

, TMemo, , - , . , ( : ), .


, var x : integer, :

constructor TCongruenceSystem.Create(var result: Integer);
begin    
  inherited Create(True);
  FreeOnTerminate := true;      
  resInner := @result;   {take the reference here}   
end;

, - , , . PInteger , , , , , .

... , , - . , , . , . , . - .

:

  { define a suitable callback signature }
  TOnCalcComplete = procedure(AResult : integer) of object;

  TCongruenceSystem = class(TThread)
  private
    Fx, Fy : integer;
    FOnCalcComplete : TOnCalcComplete;
  protected
    procedure Execute; override;
  public
    constructor Create(x,y: integer);
    property OnCalcComplete : TOnCalcComplete read FOnCalcComplete write FOnCalcComplete;
  end;

constructor TCongruenceSystem.Create(x: Integer; y: Integer);
begin
  inherited Create(true);
  FreeOnTerminate := true;
  Fx := x;
  Fy := y;
end;

procedure TCongruenceSystem.Execute;
var
  sumOfxy : integer;
begin
  sumOfxy := Fx + Fy;
  sleep(3000);        {take some time...}
  if Assigned(FOnCalcComplete) then
    Synchronize(procedure
                begin
                  FOnCalcComplete(sumOfxy);
                end);
end;

:

{ implement an event handler ... }
procedure TForm1.CalcComplete(AResult: Integer);
begin
  ShowMessage(IntToStr(AResult));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  LCongruenceSystem : TCongruenceSystem;
begin
  LCongruenceSystem := TCongruenceSystem.Create(5, 2);
  LCongruenceSystem.OnCalcComplete := CalcComplete;  { attach the handler }
  LCongruenceSystem.Start;
end;

, Synchronize Queue. , , ( ...):

, TThread.Queue

FreeOnTerminate: = True .

+3

Source: https://habr.com/ru/post/1684806/


All Articles