Hallo,<br>ich habe ein Problem mit Synchronize. Wenn ich im untenstehenden Quelltext in der Execute Methode Synchronize verwende, werden die beiden Labels nicht parallel hochgezählt. Ohne Synchronize läuft es parallel. Mir ist aber klar, dass ich die Sache synchronizieren muß. Kann mir jemand erklären, warum es mit Synchronize nicht parallel läuft ?<br>:-) Jens Schumann
<pre><font face="Verdana" size="2">
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, syncobjs;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
procedure CountingLabel(aLabel : TLabel);
public
{ Public-Deklarationen }
end;
TThreadTest = class(TThread)
private
{ Private-Deklarationen }
FForm : TForm1;
FLabel : TLabel;
procedure CountLabel;
protected
constructor Create(CreateSuspended : Boolean;aForm : TForm1;aLabel : TLabel);
procedure Execute; override;
end;
var Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
procedure TForm1.CountingLabel(aLabel: TLabel);
var
iCnt : Integer;
begin
For iCnt:=0 to 5000 do
begin
aLabel.Caption:=IntToStr(iCnt);
Application.ProcessMessages;
end;
end;
{ TThreadTest }
procedure TThreadTest.CountLabel;
begin
FForm.CountingLabel(FLabel);
end;
constructor TThreadTest.Create(CreateSuspended: Boolean; aForm: TForm1;aLabel : TLabel);
begin
Inherited Create(CreateSuspended);
FForm:=aForm;
FLabel:=aLabel;
FreeOnterminate:=True;
Resume;
end;
procedure TThreadTest.Execute;
begin
// Synchronize(CountLabel);
CountLabel;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aThreadTest : TThreadTest;
begin
aThreadTest:=TThreadTest.Create(True,Self,Label1);
aThreadTest:=TThreadTest.Create(True,Self,Label2);
end;
end.</font></pre>
<pre><font face="Verdana" size="2">
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, syncobjs;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
procedure CountingLabel(aLabel : TLabel);
public
{ Public-Deklarationen }
end;
TThreadTest = class(TThread)
private
{ Private-Deklarationen }
FForm : TForm1;
FLabel : TLabel;
procedure CountLabel;
protected
constructor Create(CreateSuspended : Boolean;aForm : TForm1;aLabel : TLabel);
procedure Execute; override;
end;
var Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
procedure TForm1.CountingLabel(aLabel: TLabel);
var
iCnt : Integer;
begin
For iCnt:=0 to 5000 do
begin
aLabel.Caption:=IntToStr(iCnt);
Application.ProcessMessages;
end;
end;
{ TThreadTest }
procedure TThreadTest.CountLabel;
begin
FForm.CountingLabel(FLabel);
end;
constructor TThreadTest.Create(CreateSuspended: Boolean; aForm: TForm1;aLabel : TLabel);
begin
Inherited Create(CreateSuspended);
FForm:=aForm;
FLabel:=aLabel;
FreeOnterminate:=True;
Resume;
end;
procedure TThreadTest.Execute;
begin
// Synchronize(CountLabel);
CountLabel;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aThreadTest : TThreadTest;
begin
aThreadTest:=TThreadTest.Create(True,Self,Label1);
aThreadTest:=TThreadTest.Create(True,Self,Label2);
end;
end.</font></pre>
Comment