Thread(100分)

  • 主题发起人 主题发起人 zhengli
  • 开始时间 开始时间
Z

zhengli

Unregistered / Unconfirmed
GUEST, unregistred user!
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;


type
TForm1 = class(TForm)
Edit1: TEdit;

Edit2: TEdit;

Button1: TButton;

procedure Button1Click(Sender: TObject);

procedure FormClose(Sender: TObject;
var Action: TCloseAction);

private
{ Private declarations }
public
{ Public declarations }
end;

TMyThread = class(TThread)
private
Number1Edit, Number2Edit: TEdit;

Number1, Number2: Integer;

Procedure ShowNumber;

protected
procedure Execute;
override;

public
Constructor Create(Edit1, Edit2: TEdit);

end;


var
Form1: TForm1;


implementation


{$R *.DFM}

{ TThread }

constructor TMyThread.Create(Edit1, Edit2: TEdit);

begin

Number1Edit := Edit1;

Number2Edit := Edit2;

Number1 := 0;

Number2 := 0;

// inherited Create(False);

Resume;

end;


procedure TMyThread.Execute;

begin

inherited;

Form1.Canvas.TextOut(100, 100, 'Why');

ShowNumber;

end;


procedure TMyThread.ShowNumber;

var
i: Integer;

begin

For i := 1 to 100000do

begin

Number1Edit.Text := IntToStr(Number1);

Number2Edit.Text := IntToStr(Number2);

Form1.Edit1.Text := IntToStr(Number1);

Form1.Edit2.Text := IntToStr(Number2);

Inc(Number1);

Inc(Number2);

Form1.Canvas.TextOut(10, 10, IntToStr(Number1 + Number2));

end;

end;


procedure TForm1.Button1Click(Sender: TObject);

begin

TMyThread.Create(Edit1, Edit2);

end;


end.


这个程序不能正常运行,总是执行不到
Execute,不知为什么,请各位帮忙查查错。
 
constructor TMyThread.Create(Edit1,Edit2:TEdit);

begin
inherited Creat(True);

Number1Edit := Edit1;

Number2Edit := Edit2;

Number1 := 0;

Number2 := 0;

Resume;

end;

 
没换行
constructor TMyThread.Create(Edit1,Edit2:TEdit);

begin

inherited Creat(True);

Number1Edit := Edit1;

Number2Edit := Edit2;

Number1 := 0;

Number2 := 0;

Resume;

end;



 
还有,把你的execute中的inherited去掉
 
to Kang:
我如果加上inherited Creat(True),会出现Abstract Error.
 
procedure TForm1.Button1Click(Sender: TObject);
var temp:TMyThread;
//加上这个;没有它程序不知道创的东西放哪
begin
temp:=TMyThread.Create(Edit1,Edit2);
end;

另外Excute中开始可以不用inherited,并且最好在开始加一句:
FreeOnTerminate:=true;
 
Kang:不用加var temp吧。除非你自己释放。用FreeOnTerminate就可以了。
zhengli:看看帮助,用到VCL的时候需要注意冲突。你new 一个thread类就
可以看到提示,不能直接给Edit1,Edit2赋值的。
 
constructor TMyThread.Create(Edit1, Edit2: TEdit);

begin

inherited Create(true);

Number1Edit := Edit1;

Number2Edit := Edit2;

Number1 := 0;

Number2 := 0;

Resume;

end;


procedure TMyThread.Execute;

var
i: Integer;

begin

inherited;

For i := 1 to 100000do

begin

Synchrosize
(ShowNumber);

Inc(Number1);

Inc(Number2);

end;

end;


procedure TMyThread.ShowNumber;

begin

Number1Edit.Text := IntToStr(Number1);

Number2Edit.Text := IntToStr(Number2);

end;


procedure TForm1.Button1Click(Sender: TObject);

begin

With TMyThread.Create(Edit1, Edit2)do

FreeOnTerminate := true;

end;

(Synchrosize可能拼错了)程序运行Resume后出错,Abstract Error.
 
我看你这个程序和delphi自带的线程例子很相似,你可以去看一下。
delphi4/demos/threads/thrddemo.dpr
 
procedure TMyThread.Execute;
什么时候冒出来一个inherited?去掉就可以了。
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
778
import
I
I
回复
0
查看
763
import
I
I
回复
0
查看
686
import
I
I
回复
0
查看
574
import
I
后退
顶部