Einzelnen Beitrag anzeigen

EdAdvokat

Registriert seit: 1. Mai 2016
Ort: Berlin
415 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#27

AW: Verständnisfrage zur Thread-Synchronisation

  Alt 13. Apr 2022, 20:04
so sieht das Ergebnis nun aus:
Delphi-Quellcode:
unit Unit1;

interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  Vcl.WinXCtrls, System.UITypes, Vcl.ExtCtrls;

type
  TTheThread = class(TThread)
  private
    fLabel : TLabel;
  public
    constructor Create(aLabel: TLabel); overload;
    procedure Execute; override;
  end;


type
  TForm1 = class(TForm)
  CounterLabel: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private-Deklarationen }
    fTheThread: TTheThread;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


constructor TTheThread.Create(aLabel: TLabel);
  begin
    fLabel := aLabel;
    inherited Create(False);
  end;

procedure TTheThread.Execute;
 var
    I1, I2: Cardinal;
  begin
    I1 := 0;
    I2 := 0;
    try
      while (not Terminated) do
        begin
          Inc(I1);
          if (I1 >= 1000) then
            begin
              Inc(I2);
              Synchronize(
                procedure
                  begin
                    FLabel.caption := I2.ToString;
                  end);
              I1 := 0;
            end;
        end;
    except
      raise;
// on e: exception do begin
// mache hier irgendetwas mit dem Fehler.
    end;
  end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
   fTheThread.Terminate;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  fTheThread := TTheThread.Create(CounterLabel);
end;
end.
Ich sehe also auf dem Formular das stets unterbrochene Hochzählen (also die Synchronisation zwischen I1 und I2)
Vielen vielen Dank für die Ausdauer mit mir Plinse.
Ich denke das war es also.
Norbert
  Mit Zitat antworten Zitat