刚接触线程,做个最简单的程序试验,为什么会报错???(很简单,白送分).(5分)

  • 主题发起人 主题发起人 koy0755
  • 开始时间 开始时间
K

koy0755

Unregistered / Unconfirmed
GUEST, unregistred user!
源程序如下,我只想试验打开thread status窗口,看是否会多一个线程.但总是失败.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
mythread = class(TThread)
private
public
procedure Execute;
override;
end;

var
Form1: TForm1;
tth:mythread;
implementation
{$R *.dfm}
procedure mythread.Execute;
begin
sleep(1000);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
tth.Create(true);
end;

end.
 
excute要有退出语句。
 
mythread = class(TThread) 前应加 type:
type
mythread = class(TThread)
 
代码似呼没什么错误
线程myThread一创建就被挂起!!!
你将true设为false;
不知道是不是你所要的效果。
 
to fftou
因为刚接触线程,只想做一个最简单的程序证实有线程的运行.没有什么目的.我试过用mythread.create(true)和false都是一创建就挂了.
 
1.要用protect 加在procedure Execute;
override;
之前
2.tth:=mythread.Create(false);
 
你一创建就Sleep(1000),不挂才怪呢,你看一看Sleep的帮助文档,它就是要线程交出CPU控制权的。
 
to ligia是不是这样?
protect procedure Execute;
override;
编释不过去?
protect 在这里的意思是什么?有什么用?
to lichaogang
我说挂了的意思是,运行异常。
 
在你的execute代码中换一种写法,比如发一个消息到Form或改变一下Form的caption.
 
线程定义
type
mythread = class(TThread)
private
public
protected
procedure Execute;
override;
end;
还是用delphi 的 thread object来做
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Txx=class(TThread)
protected
procedure Execute;
override;
public
constructor create();
end;

var
Form1: TForm1;
xx:Txx;
implementation
{$R *.dfm}
constructor Txx.create();
begin
inherited create(false);
freeonterminate:=true;//线程终止时自动删除对象
end;

procedure TXX.Execute;
begin
beep();
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
xx:=Txx.create();
end;

end.
 
多人接受答案了。
 
后退
顶部