下面多线程代码为什么会错误,请各位老大指教:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
type
TMythread = class(TThread)
protected
procedure Execute; override;
end;
procedure TMythread.Execute;
var
i,a:Integer;
begin
a:=0;
for I := 0 to 5000000 do
begin
a:=a+1;
Form1.Label1.Caption:=IntToStr(a);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TMythread.Create(False);
end;
end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
type
TMythread = class(TThread)
protected
procedure Execute; override;
end;
procedure TMythread.Execute;
var
i,a:Integer;
begin
a:=0;
for I := 0 to 5000000 do
begin
a:=a+1;
Form1.Label1.Caption:=IntToStr(a);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TMythread.Create(False);
end;
end.
Sleep(10);
就不会出错了。。难道真的是循环的速度过快导致数据溢出吗??????