Registriert seit: 1. Mai 2016
Ort: Berlin
419 Beiträge
Delphi 10.2 Tokyo Professional
|
AW: Verständnisfrage zur Thread-Synchronisation
13. Apr 2022, 11:17
Hallo Strahli, hier mein Versuch Deine Aufgabenstgellung zu lösen. Sicher nicht das Gelbe vom Osterei.
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, Vcl.ExtCtrls;
type
TheThread = class(TThread)
private
procedure DoIt;
public
procedure Execute; override;
end;
type
TForm1 = class(TForm)
CounterLabel: TLabel;
StartThreadBtn: TButton;
btnclose: TButton;
procedure btncloseClick(Sender: TObject);
procedure StartThreadBtnClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TheThread.DoIt;
var Zaehler1, Zaehler2, erg1: Integer;
begin
for Zaehler1 := 0 to 1000000 do
erg1:=Zaehler1;
Zaehler2:=1;
Zaehler2 := Zaehler2 + 1;
Zaehler1:=0;
begin
Form1.counterlabel.Caption := Zaehler2.tostring;
end;
end;
procedure TheThread.Execute;
begin
Synchronize(DoIt);
end;
procedure TForm1.btncloseClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.StartThreadBtnClick(Sender: TObject);
var
Thread: TheThread;
I: integer;
begin
try
Thread := TheThread.Create(True);
Thread.FreeOnTerminate := true;
Thread.Start;
Thread.ShutdownThread;
except
on E: Exception do
begin
MessageDlg(E. Message, mtError, [mbOK], -1);
end;
end;
end;
end.
Norbert
|
|
Zitat
|