多线程有两窗体如下:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,unit2;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
A1:TThread;
begin
A1:=tThread.create(False);
end;end.unit Unit2;interfaceuses
Classes,SysUtils;type
A1 = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;implementationuses unit1;{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure A1.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ A1 }procedure A1.Execute;
var
i:integer;
begin
{ Place thread code here }
for i:=1 to 200000000 do
form1.edit1.text:=inttostr(i);end;end.点击form1.Button1出错?什么原因
var
A:A1;
begin
A:=A1.create(False);
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}uses TestThread;procedure TForm1.Button1Click(Sender: TObject);
var
NewThread: TTestThread;
begin
NewThread := TTestThread.Create(False);
end;end.
unit TestThread;interfaceuses
Classes;type
TTestThread = class(TThread)
private
Answer: integer;
{ Private declarations }
protected
procedure GiveAnswer;
procedure Execute; override;
end;implementationuses SysUtils, Unit1;{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TTestThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TTestThread }procedure TTestThread.GiveAnswer;
begin
Form1.Edit1.Text := IntToStr(Answer);
end;procedure TTestThread.Execute;
var
i: integer;
begin
FreeOnTerminate := True;
for i:=1 to 20000 do
begin
if Terminated then
Break;
inc(Answer, Round(Abs(Sin(Sqrt(i)))));
Synchronize(GiveAnswer);
end;
{ Place thread code here }
end;end.
var
a: A1;//A1:TThread;
begin
a:=A1.create(false);//A1:=tThread.create(False);
end;