为什么线程不再创建了呢?--------在线等待! ( 积分: 70 )

  • 主题发起人 主题发起人 crasyProg
  • 开始时间 开始时间
C

crasyProg

Unregistered / Unconfirmed
GUEST, unregistred user!
我使用线程向导创建了一个线程类,在线程Destroy时我向主线程postmessage,然后主窗体捕捉到消息后马上再创建一个线程,可是为什么运行一段时间不再创建了呢?源码如下:
线程类代码:
private
myControl:Tcontrol;//控件名称假设的
destruct Mythread.destroy;
begin
postmessage(fom1.handle,wm_threadMsg1,0,0);
inherit;
end;
MyThread.Execute;
begin
try
myControl:=Tcontrol.create(nil);
... //myControl一些属性付值
myControl.timeout=1000;
myControl.do();
myControl.OnSucess=myThreadSucess;
while not terminateddo
application.processmessage;
finally
myControl.free;
end;
end;
procedure MyThread.myThreadSucess(sender:Tobject,..)
begin
postmessage(form1.handle,wm_thredMsg2,1,0);
terminate;//新添代码
end;
主窗体代码:
const
wm_threadMsg1=wm_message+99;
wm_threadMsg2=wm_message+100;
procedure wmthreadmsg(msg:Tmessage):messages wm_threadMsg1;
begin
MyThread.create;
..
application.processmessage;
end;
 
我使用线程向导创建了一个线程类,在线程Destroy时我向主线程postmessage,然后主窗体捕捉到消息后马上再创建一个线程,可是为什么运行一段时间不再创建了呢?源码如下:
线程类代码:
private
myControl:Tcontrol;//控件名称假设的
destruct Mythread.destroy;
begin
postmessage(fom1.handle,wm_threadMsg1,0,0);
inherit;
end;
MyThread.Execute;
begin
try
myControl:=Tcontrol.create(nil);
... //myControl一些属性付值
myControl.timeout=1000;
myControl.do();
myControl.OnSucess=myThreadSucess;
while not terminateddo
application.processmessage;
finally
myControl.free;
end;
end;
procedure MyThread.myThreadSucess(sender:Tobject,..)
begin
postmessage(form1.handle,wm_thredMsg2,1,0);
terminate;//新添代码
end;
主窗体代码:
const
wm_threadMsg1=wm_message+99;
wm_threadMsg2=wm_message+100;
procedure wmthreadmsg(msg:Tmessage):messages wm_threadMsg1;
begin
MyThread.create;
..
application.processmessage;
end;
 
我不知道这是为什么,你用另一个办法能不能解决你的问题呢?
在此线程的OnTerminate事件中postmessage而不是在destroy中。
一般来说用OnTerminate来处理线程结束后的事情比较好。
 
说的有理.
我试过了,好像还是不行啊?
各位朋友帮帮忙阿,小弟非常感谢!
 
怎么没有人回答呢,我可是在线等待,自己顶一下!!
 
你把你在Destroy里的
postmessage(fom1.handle,wm_threadMsg1,0,0);
移到你的 Execute 的最后看看呢?
还有你的线程自己调用自己的Terminate不知道会不会有问题,
你另外加一个变量来表征你的 Control 是否成功,比如 blDone
在 Execute 中
blDone := false;
while (not terminated) and (not blDone)do
application.processmessage;
在你的myThreadSucess中置 blDone := true;
 
Let Me Try!
 

Similar threads

L
回复
9
查看
285
lcl_003
L
回复
3
查看
351
迷惘的人
C
回复
3
查看
367
coolingxyz
C
W
回复
2
查看
266
lp414
L
后退
顶部