我很菜的。 下面有一段,但出错。 帮看看。 unit Unit2; interface uses Classes,SysUtils; type MyThread = class(TThread) private { Private declarations } procedure AddLineToMemo; protected procedure Execute; override; end;implementation uses Unit1; { Important: Methods and properties of objects in VCL or CLX can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure MyThread.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ MyThread }procedure MyThread.AddLineToMemo; begin form1.memo1.lines.add('============='); end;procedure MyThread.Execute; var i :integer; begin { Place thread code here } for i := 1 to 100 do Synchronize(AddLineToMemo);end;end.在单击事件中有。 var mythread1:MyThread; begin mythread1.create(true); mythread1.FreeOnTerminate:=true; mythread1.resume; end;但 那个mythread1.resume;的时候出错了。什么内存地址不应该为xxxxxx. 编译时有一warning.说mythread1没有初始化。 还是没有成功。请高手再次,赐教。感谢。
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TMyThread = class(TThread) private FMemo:TMemo; protected procedure Execute; override; public constructor Create(Memo:TMemo); destructor Destroy; override; end; TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var MyThread :TMyThread; begin MyThread := TMyThread.Create(Memo1); end;{ TMyThread }constructor TMyThread.Create(Memo: TMemo); begin FMemo := Memo; FreeOnTerminate := False; inherited Create(False); end;destructor TMyThread.Destroy; begin inherited; end;procedure TMyThread.Execute; var i:integer; begin inherited; for i:=1 to 100 do FMemo.Lines.Add('Lines'+IntToStr(i)); end;end.
下面有一段,但出错。
帮看看。
unit Unit2;
interface
uses
Classes,SysUtils;
type
MyThread = class(TThread)
private
{ Private declarations }
procedure AddLineToMemo;
protected
procedure Execute; override; end;implementation
uses Unit1;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure MyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ MyThread }procedure MyThread.AddLineToMemo;
begin
form1.memo1.lines.add('=============');
end;procedure MyThread.Execute;
var
i :integer;
begin
{ Place thread code here }
for i := 1 to 100 do
Synchronize(AddLineToMemo);end;end.在单击事件中有。
var
mythread1:MyThread;
begin
mythread1.create(true);
mythread1.FreeOnTerminate:=true;
mythread1.resume;
end;但
那个mythread1.resume;的时候出错了。什么内存地址不应该为xxxxxx.
编译时有一warning.说mythread1没有初始化。
还是没有成功。请高手再次,赐教。感谢。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TMyThread = class(TThread)
private
FMemo:TMemo;
protected
procedure Execute; override;
public
constructor Create(Memo:TMemo);
destructor Destroy; override;
end; TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
MyThread :TMyThread;
begin
MyThread := TMyThread.Create(Memo1);
end;{ TMyThread }constructor TMyThread.Create(Memo: TMemo);
begin
FMemo := Memo;
FreeOnTerminate := False;
inherited Create(False);
end;destructor TMyThread.Destroy;
begin inherited;
end;procedure TMyThread.Execute;
var
i:integer;
begin
inherited;
for i:=1 to 100 do
FMemo.Lines.Add('Lines'+IntToStr(i));
end;end.
结帖。