...让我迷茫的线程,如何处理线程结束问题.....

preetygirl 2003-09-11 03:32:33
procedure sms_thread.Execute;
var s:string;
begin
freeonterminate:=true;
s:=dm1.sms_check(); //执行一个函数
if terminated then exit;
end;

调用该线程
procedure TForm1.Button1Click(Sender: TObject);
begin
temp:=1;
while temp=1 do
begin
thred.sms_thread.Create(false); //创建线程
thred.sms_thread.onOermminate:=threadDone;//这里出错了,我不知道该怎么办
end;

这个程序是要用来检查数据库是否有变化的,需要不停的执行sms_check()函数,所以我才想到用线程来调用它,但程序运行时老出错,我刚学的delphi,请各位大哥指教!!!
end;
...全文
42 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hiflower 2003-09-28
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
begin
temp:=1;
while temp=1 do
begin
thred.sms_thread.Create(false); //创建线程
thred.sms_thread.onOermminate:=threadDone;//这里出错了,我不知道该怎么办
end;

改为:
procedure TForm1.Button1Click(Sender: TObject);
begin
temp:=1;
while temp=1 do
begin
with thred.sms_thread.Create(True) do //这里我猜想 thred 是你定义 sms_thread 类的一个单元名称
begin
onOermminate:=threadDone;
Resume;
end;
end;
end;

不过你这种做法很不好,在不停地创建线程,资源要被耗尽了。
还是用我上面的方法吧。
我上面写错了一句:
published
property OnDBChange:TNotifyEvent read FOnDBChange write FOnDBChange;// 写一个数据库状态变化的事件

eaglezhao 2003-09-28
  • 打赏
  • 举报
回复
不知道上面这位老兄贴这么多代码干什么。
从搂主的代码看,是基本概念不清楚的问题,
且看
thred.sms_thread.onOermminate:=threadDone;//这里出错了,我不知道该怎么办
你怎么可以这样设置呢?thred.sms_thread只是一个类,而不是类对象,你的原意应该是要将threadDone赋给
“thred.sms_thread.Create(false); //创建线程”所创建的线程对象,但是却用错了。
所以会出现错误。
hiflower 2003-09-12
  • 打赏
  • 举报
回复
type
sms_thread=class(TThread);
...//你的东西
private
FOnDBChange:TNotifyEvent;
published
OnDBChange:TNotifyEvent read FOnDBChange write FOnDBChange;// 写一个数据库状态变化的事件
end;

procedure sms_thread.Execute;
var s:string;
begin
while not terminated do
begin
s:=dm1.sms_check(); //执行一个函数
if 状态变化 then
if Assigned(OnDBChange) then
OnDBChange(Self);
end;
end;

var
AThread:SMS_Thread;
procedure TForm1.Button1Click(Sender: TObject);
begin
AThread:=SMS_Thread.Create(True);
with AThread do
begin
OnTermminate := threadDone;
OnDBChange:=DBChange;//你要声明一个TNotifyEvent 类型的 DBChange 过程
FreeOnTerminate:=True;
Resume;
end;
end;

procedure TForm1.Button2Click(Sender:TObject);
begin
AThread.Terminate;
end;

procedure TForm1.DBChange(Sender:TObject);
begin
...//状态变化的处理
end;
preetygirl 2003-09-12
  • 打赏
  • 举报
回复
上面的创建的线程占用的资源,怎么释放掉?


没有人理我??。。。。。
同桌老王 2003-09-12
  • 打赏
  • 举报
回复
//一个delphi自带的例子,很清楚的。一定要等你处理完毕或者有什么事件触发,才会停止线程的。
unit Pg1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls, Pg2;

const
WM_ThreadDoneMsg = WM_User + 8;

type
TForm1 = class(TForm)
ProgressBar1: TProgressBar;
ProgressBar2: TProgressBar;
Button1: TButton;
Button2: TButton;
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
Bevel1: TBevel;
Bevel2: TBevel;
Label1: TLabel;

Label2: TLabel;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBar2Change(Sender: TObject);
procedure FormDestroy(Sender: TObject);

private
{ Private declarations }
MyThread1 : TMyThread; // thread number 1
MyThread2 : TMyThread; // thread number 2
Thread1Active : boolean; // used to test if thread 1 is active
Thread2Active : boolean; // used to test if thread 2 is active
procedure ThreadDone(var AMessage : TMessage); message WM_ThreadDoneMsg; // Message to be sent back from thread when its done
public
{ Public declarations }

end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject); // Create Thread 1
{ The thread will destroy iteself when it is done executing because FreeOnTerminate is set to true.
The first paramter is the priority, and the second is the progressbar to update.
}
begin
if (MyThread1 = nil) or (Thread1Active = false) then // make sure its not already running

begin
MyThread1 := TMyThread.CreateIt(TrackBar1.Position, ProgressBar1);
Thread1Active := true;
end
else
ShowMessage('Thread still executing');
end;

procedure TForm1.Button2Click(Sender: TObject); // Create Thread 2
begin
if (MyThread2 = nil) or (Thread2Active = false) then // make sure its not already running
begin
MyThread2 := TMyThread.CreateIt(TrackBar2.Position, ProgressBar2);

Thread2Active := true;
end
else
ShowMessage('Thread still executing');
end;

procedure TForm1.Button3Click(Sender: TObject); // Terminate Thread 1
begin
if (MyThread1 <> nil) and (Thread1Active = true) then // check to see if it is running
MyThread1.Terminate
else
ShowMessage('Thread not started');
end;

procedure TForm1.Button4Click(Sender: TObject); // Terminate Thread 2

begin
if (MyThread2 <> nil) and (Thread2Active = true) then // check to see if it is running
MyThread2.Terminate
else
ShowMessage('Thread not started');
end;

procedure TForm1.ThreadDone(var AMessage: TMessage); // keep track of when and which thread is done executing
begin
if ((MyThread1 <> nil) and (MyThread1.ThreadID = cardinal(AMessage.WParam))) then

begin
Thread1Active := false;
end;
if ((MyThread2 <> nil) and (MyThread2.ThreadID = cardinal(AMessage.WParam))) then
begin
Thread2Active := false;
end;
end;


procedure TForm1.FormCreate(Sender: TObject); // initialize to zero
begin
Thread1Active := false;
Thread2Active := false;
end;


procedure TForm1.TrackBar1Change(Sender: TObject); // set Thread 1 Priority

begin
if (MyThread1 <> nil) and (Thread1Active = true) then
MyThread1.priority := TThreadPriority(TrackBar1.Position);
end;

procedure TForm1.TrackBar2Change(Sender: TObject); // set Thread 2 Priority
begin
if (MyThread2 <> nil) and (Thread2Active = true) then
MyThread2.priority := TThreadPriority(TrackBar2.Position);
end;


procedure TForm1.FormDestroy(Sender: TObject); // Terminate any threads still running

begin
if (MyThread1 <> nil) and (Thread1Active = true) then
begin
MyThread1.Terminate;
MyThread1.WaitFor; // wait for it to terminate
end;
if (MyThread2 <> nil) and (Thread2Active = true) then
begin
MyThread2.Terminate;
MyThread2.WaitFor;
end;
end;

end.
jpyc 2003-09-12
  • 打赏
  • 举报
回复
参考一下:

http://218.56.11.178:8020/web/technology.aspx

-》在DELPHI中建立线程的方法

http://expert.csdn.net/Expert/topic/2178/2178905.xml?temp=.947735
preetygirl 2003-09-11
  • 打赏
  • 举报
回复
up 一下!!!!
preetygirl 2003-09-11
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
begin
temp:=1;
while temp=1 do
begin
thred.sms_thread.Create(true);
end;

我把代码改成上面的样子了,不去管线程结束时的处理了,让它自生自灭吧。现在我只想新创建的线程释放掉,bphoenix(吸血凤凰) 的代码我不知道怎么用,能详细点吗?我是delphi初学者,^_^!谢谢!!!


end;
bphoenix 2003-09-11
  • 打赏
  • 举报
回复
线程创建时的参数False代表创建后立刻执行Execute,True代表创建后先挂起,只有调Resume后才能执行Execute,故建议你把onOermminate := threadDone;写到sms_thread的Create函数中,另外结束线程时建议用以下语句:
Terminate;
WaitFor;
Free;
preetygirl 2003-09-11
  • 打赏
  • 举报
回复
不知道为什么,onTerminate := threadDone;这行出错。
我把它改为thred.sms_thread.onTerminate 却发现dephi弹出的下拉单里没有onterminate这个事件。

我晕!!天啊!!
byc6352 2003-09-11
  • 打赏
  • 举报
回复
lxpbuaa(桂枝香在故国晚秋) 说的对。
lxpbuaa 2003-09-11
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
begin
while temp=1 do
begin
with sms_thread.Create(True);
onOermminate := threadDone;
Resume;
end;
end;

————————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
————————————————————————————————————

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧