强行关闭线程后,程序无法退出(在线等待,谢谢,分不够再加!)(100分)

A

aningok

Unregistered / Unconfirmed
GUEST, unregistred user!
//线程的执行体
procedure TMakePDFThread.Execute;
begin
while not terminteddo

begin
//进行远程数据访问,时间可能很长,甚至无法返回
end;
end;
//所以想提供给用户一个立即结束线程的按钮
//按钮中代码:
if Assigned(makeProcessThread) then
begin
TerminateThread(makePDFThread.Handle,0);
CloseHandle(makePDFThread.Handle);
makePDFThread := nil;
end;
//用户点击按钮可以强行结束线程
//但是当用户想退出程序时不行
调用 Application.Terminate;
系统根本就不能结束
 
本来想用
makePDFThread.terminate;
makePDFThread.free;
结束的!
但是:
procedure TMakePDFThread.Execute;
begin
while not terminteddo

begin
//进行远程数据访问,时间可能很长,甚至无法返回
[red]这里将执行很长时间甚至无法返回,那么方法就不会去检查terminated属性了![/red]
end;
end;
 
尽量不要强行终止线程, 应当让线程自行退出,这是Borland的Thread对象包含的建议
 
dpr里加上try
 
多谢 LiChaoHui ,soul 兄!
to LiChaoHui:
我也不想强行结束啊,但是线程会去访问网络,而服务器那端可能会锁定,导致
客户端永久等待,不这样做有什么办法啊 ?

 
来自:soul, 时间:2003-5-25 22:33:00, ID:1894411
dpr里加上try
----------------
线程执行时没有超时时间,不会收到异常,请问用try 是怎么样达到目的的?谢谢
 
这个刚刚讨论过:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1076676
 
讨论过,好象没什么结果啊
 

if Assigned(makeProcessThread) then
begin
TerminateThread(makePDFThread.Handle,0);
CloseHandle(makePDFThread.Handle);
makePDFThread := nil;
end;

Halt(0);不能退出吗?
有什么错啊?
 
多谢:wolaixue 兄!!!
我之前是用 application.terminate 系统更本就不响应这条命令
用了halt(0)确实能退出程序了!!!
但是会报错“无效的窗口句柄”
请继续给予帮忙,不胜感激!


 
错误代码:
System Error . Code :1400.
请各位帮忙诊断一下,谢谢。
 
是不是DLL?
 
用halt(0)程序并不能正常关闭
会一直弹出错误窗口
最后一个警告是:Runtime error 216 at 00989892
 
线程中有用到
ShellExecute(mainform.Handle,'print',pchar(fileName),nil,nil,SW_SHOWMINNOACTIVE);
暂时还没调dll,以后肯定要调。 :(
 
option里把出错信息1400关掉。没办法,一般用到一些ole后,和系统挂得比较紧密。
或者你先杀进程了。但是如果和系统挂得比较紧密也会有问题。
 
ShellExecute(mainform.Handle,'print',pchar(fileName),nil,nil,SW_SHOWMINNOACTIVE);
先将mainform.Handle改成0试试。
 
我脱离delphi环境,在直接点击打开的exe文件中操作也会报1400错误!
请问在option如何关掉出错信息?关掉后直接执行exe也不会报错了吗?
 
to wolaixue:
把mainform.handle 改为0后错误依旧
 
先这样试试:
procedure TMakePDFThread.Execute;
begin
while Truedo
Sleep(0);
end;
这样测试可以确定错误是不是线程本身引起的!
如果这样测试还有问题,建议复制Forms等单元到你的工程目录,跟踪到具体出错位置,
很有可能是Form.Free引起的。(我遇到过)
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
721
import
I
顶部