在主界面创建后,创建线程,在主界面销毁前,销毁线程。(线程需要操纵主界面的VCL控件)
线程的EXEC中这样写的:
while not terminate do
begin
。。。
end;
但是,现在存在这样的问题。
如果我用下面的方法释放线程:
threada。terminatethread;
//Threada。suspend;
threada。terminate;
threada :=nil
会出现线程对象被释放了,即threada,但是线程本身没有被释放。
如果用下面的方法:
threada。suspend;
threada。terminate;
threada :=nil;
把线程释放掉,但是重新建立线程进行操作时,第一次会报错。
基本就是这个样子,请大虾们办忙解决啊
提供正确的线程释放和线程对象的释放方法。
23 个解决方案
#1
Determines whether the thread object is automatically destroyed when the thread terminates.
Delphi syntax:
property FreeOnTerminate: Boolean;
C++ syntax:
__property bool FreeOnTerminate = {read=FFreeOnTerminate, write
=FFreeOnTerminate, nodefault};
Description
Set FreeOnTerminate to true if you don抰 want to explicitly destroy threads after they finish executing. When FreeOnTerminate is false, the thread object must be explicitly destroyed in application code.
Warning: When FreeOnTerminate is true, the Execute method may run and then free the thread before your application can execute the next line of code. Thus, you should not call any methods of the thread object when FreeOnTerminate is true unless you create the thread in a suspended state.
Delphi syntax:
property FreeOnTerminate: Boolean;
C++ syntax:
__property bool FreeOnTerminate = {read=FFreeOnTerminate, write
=FFreeOnTerminate, nodefault};
Description
Set FreeOnTerminate to true if you don抰 want to explicitly destroy threads after they finish executing. When FreeOnTerminate is false, the thread object must be explicitly destroyed in application code.
Warning: When FreeOnTerminate is true, the Execute method may run and then free the thread before your application can execute the next line of code. Thus, you should not call any methods of the thread object when FreeOnTerminate is true unless you create the thread in a suspended state.
#2
FreeOnTerminate:=true;
#3
这个方法用了,在create里面,但是还是这种情况,请大家继续发言
#4
GetExitCodeThread(Mythread.Handle, aa);
TerminateThread(Mythread.Handle,aa)
TerminateThread(Mythread.Handle,aa)
#5
FreeOnTerminate:=true; 表示 THREAD在使用完毕从MEM中FREE掉
如果FreeOnTerminate:=FALSE
从内存FREE 是
THREAD.terminate;
THREAD.FREE;
如果FreeOnTerminate:=FALSE
从内存FREE 是
THREAD.terminate;
THREAD.FREE;
#6
THREAD.terminate;只是退出线程,没有释放,要释放需要THREAD.FREE;
#7
THREAD.terminate;——停止线程
Thread.WaitFor;——等待线程完全停止
Thread.Free;——释放线程
因为停止一个线程是需要时间的,不调用WaitFor,直接使用Free可能会由于执行速度太快导致线程还没有完全停止,所以Free就会出问题!!!
Thread.WaitFor;——等待线程完全停止
Thread.Free;——释放线程
因为停止一个线程是需要时间的,不调用WaitFor,直接使用Free可能会由于执行速度太快导致线程还没有完全停止,所以Free就会出问题!!!
#8
55555,为什么没有更好的答案,难道CSDN开始败落了〉?〉〉〉
#9
Delphi_Li(Delphi Li) 应该好用
#10
up
#11
Thread.Free;
#12
同意Delphi_Li(Delphi Li) 的观点。
threada。terminate;
最好用WaitFor等一下,等线程真的退出了再做下一步动作。
threada :=nil;
threada。terminate;
最好用WaitFor等一下,等线程真的退出了再做下一步动作。
threada :=nil;
#13
同意Delphi_Li(Delphi Li)
线程退出一定要等待
线程退出一定要等待
#14
只要设置了 FreeOnTerminate 为 True,则不需要显式地调用 Thread.Free
#15
如果在线程函数中这样写了:
Thread.Execute()
begin
while not self.Terminated do begin
.....
end;
end;
首先实际线程和Delphi中定义的线程对象Thread不是一回事儿
如果要释放实际线程,则调用线程对象Thread.Terminate,该方法将线程对象的Terminated属性置为True,则由于线程函数中检测到Terminated值,会结束线程方法;实际的线程也会稍后结束。
如果释放线程对象Thread,则在释放实际线程后,调用Thread.free即可
Thread.Execute()
begin
while not self.Terminated do begin
.....
end;
end;
首先实际线程和Delphi中定义的线程对象Thread不是一回事儿
如果要释放实际线程,则调用线程对象Thread.Terminate,该方法将线程对象的Terminated属性置为True,则由于线程函数中检测到Terminated值,会结束线程方法;实际的线程也会稍后结束。
如果释放线程对象Thread,则在释放实际线程后,调用Thread.free即可
#16
如果是在关闭程序前的话
可以直接调用Win32 API函数TerminateThread,更为暴力些
常常这样用的:TerminateThread(myThread.Handle,0);
可以直接调用Win32 API函数TerminateThread,更为暴力些
常常这样用的:TerminateThread(myThread.Handle,0);
#17
个人实践观点
由于delphi6的TThread destroy函数是这样
if (FThreadID <> 0) and not FFinished then
begin
Terminate;
if FCreateSuspended then
Resume;
WaitFor;
end;
对于 FreeOnTerminate=true的可以这样
try//避免已释放的线程对象再被引用
FThread.Terminate;
while FThread.suspended do
FThread.Resume;//彻底resume
excepte
end
对于 FreeOnTerminate=false的可以这样
FThread.Terminate;
while FThread.suspended do
FThread.Resume;
FThread.Free;
由于delphi6的TThread destroy函数是这样
if (FThreadID <> 0) and not FFinished then
begin
Terminate;
if FCreateSuspended then
Resume;
WaitFor;
end;
对于 FreeOnTerminate=true的可以这样
try//避免已释放的线程对象再被引用
FThread.Terminate;
while FThread.suspended do
FThread.Resume;//彻底resume
excepte
end
对于 FreeOnTerminate=false的可以这样
FThread.Terminate;
while FThread.suspended do
FThread.Resume;
FThread.Free;
#18
如果是在关闭程序前的话
可以直接调用Win32 API函数TerminateThread,更为暴力些
常常这样用的:TerminateThread(myThread.Handle,0);
这样子可以杀掉线程,但是为什么没有释放线程所占用的资源???而且,再次创建的时候,有时候导致系统死锁。
可以直接调用Win32 API函数TerminateThread,更为暴力些
常常这样用的:TerminateThread(myThread.Handle,0);
这样子可以杀掉线程,但是为什么没有释放线程所占用的资源???而且,再次创建的时候,有时候导致系统死锁。
#19
大家继续啊,这么长时间了,呵呵,Delphi的线程这么难用?还是我写的有些问题,大家说得我都试验过了,还没有完全解决。希望大家多多帮忙啊
#20
threada。terminate;
sleep(100);
threada :=nil;
sleep(100);
threada :=nil;
#21
//通知线程终止
FThread.Terminate;
//等待线程退出
while WaitForSingleObject(FThread1.Handle, 0) <> WAIT_OBJECT_0 do
for J := 0 to 1000 do
//如果程序具有Application特性(如EXE),可直接运行下面;否则,如(COM中),请自己重
//写ProcessMessage中的代码
Application.ProcessMessage;
//如果不是FreeOnTerminate的,加上这一句
FThread1.Free;
#22
constructor TMyThread.Create;
begin
inherited Create(false);
FreeOnTerminate:=True;;
end;
begin
inherited Create(false);
FreeOnTerminate:=True;;
end;
#23
上面是自动退出
受控退出:
var
MyThread1:TMyThread;
创建
MyThread1:=TMyThread.Create(false);
释放
MyThread1.Suspend;
MyThread1.Free;
受控退出:
var
MyThread1:TMyThread;
创建
MyThread1:=TMyThread.Create(false);
释放
MyThread1.Suspend;
MyThread1.Free;
#1
Determines whether the thread object is automatically destroyed when the thread terminates.
Delphi syntax:
property FreeOnTerminate: Boolean;
C++ syntax:
__property bool FreeOnTerminate = {read=FFreeOnTerminate, write
=FFreeOnTerminate, nodefault};
Description
Set FreeOnTerminate to true if you don抰 want to explicitly destroy threads after they finish executing. When FreeOnTerminate is false, the thread object must be explicitly destroyed in application code.
Warning: When FreeOnTerminate is true, the Execute method may run and then free the thread before your application can execute the next line of code. Thus, you should not call any methods of the thread object when FreeOnTerminate is true unless you create the thread in a suspended state.
Delphi syntax:
property FreeOnTerminate: Boolean;
C++ syntax:
__property bool FreeOnTerminate = {read=FFreeOnTerminate, write
=FFreeOnTerminate, nodefault};
Description
Set FreeOnTerminate to true if you don抰 want to explicitly destroy threads after they finish executing. When FreeOnTerminate is false, the thread object must be explicitly destroyed in application code.
Warning: When FreeOnTerminate is true, the Execute method may run and then free the thread before your application can execute the next line of code. Thus, you should not call any methods of the thread object when FreeOnTerminate is true unless you create the thread in a suspended state.
#2
FreeOnTerminate:=true;
#3
这个方法用了,在create里面,但是还是这种情况,请大家继续发言
#4
GetExitCodeThread(Mythread.Handle, aa);
TerminateThread(Mythread.Handle,aa)
TerminateThread(Mythread.Handle,aa)
#5
FreeOnTerminate:=true; 表示 THREAD在使用完毕从MEM中FREE掉
如果FreeOnTerminate:=FALSE
从内存FREE 是
THREAD.terminate;
THREAD.FREE;
如果FreeOnTerminate:=FALSE
从内存FREE 是
THREAD.terminate;
THREAD.FREE;
#6
THREAD.terminate;只是退出线程,没有释放,要释放需要THREAD.FREE;
#7
THREAD.terminate;——停止线程
Thread.WaitFor;——等待线程完全停止
Thread.Free;——释放线程
因为停止一个线程是需要时间的,不调用WaitFor,直接使用Free可能会由于执行速度太快导致线程还没有完全停止,所以Free就会出问题!!!
Thread.WaitFor;——等待线程完全停止
Thread.Free;——释放线程
因为停止一个线程是需要时间的,不调用WaitFor,直接使用Free可能会由于执行速度太快导致线程还没有完全停止,所以Free就会出问题!!!
#8
55555,为什么没有更好的答案,难道CSDN开始败落了〉?〉〉〉
#9
Delphi_Li(Delphi Li) 应该好用
#10
up
#11
Thread.Free;
#12
同意Delphi_Li(Delphi Li) 的观点。
threada。terminate;
最好用WaitFor等一下,等线程真的退出了再做下一步动作。
threada :=nil;
threada。terminate;
最好用WaitFor等一下,等线程真的退出了再做下一步动作。
threada :=nil;
#13
同意Delphi_Li(Delphi Li)
线程退出一定要等待
线程退出一定要等待
#14
只要设置了 FreeOnTerminate 为 True,则不需要显式地调用 Thread.Free
#15
如果在线程函数中这样写了:
Thread.Execute()
begin
while not self.Terminated do begin
.....
end;
end;
首先实际线程和Delphi中定义的线程对象Thread不是一回事儿
如果要释放实际线程,则调用线程对象Thread.Terminate,该方法将线程对象的Terminated属性置为True,则由于线程函数中检测到Terminated值,会结束线程方法;实际的线程也会稍后结束。
如果释放线程对象Thread,则在释放实际线程后,调用Thread.free即可
Thread.Execute()
begin
while not self.Terminated do begin
.....
end;
end;
首先实际线程和Delphi中定义的线程对象Thread不是一回事儿
如果要释放实际线程,则调用线程对象Thread.Terminate,该方法将线程对象的Terminated属性置为True,则由于线程函数中检测到Terminated值,会结束线程方法;实际的线程也会稍后结束。
如果释放线程对象Thread,则在释放实际线程后,调用Thread.free即可
#16
如果是在关闭程序前的话
可以直接调用Win32 API函数TerminateThread,更为暴力些
常常这样用的:TerminateThread(myThread.Handle,0);
可以直接调用Win32 API函数TerminateThread,更为暴力些
常常这样用的:TerminateThread(myThread.Handle,0);
#17
个人实践观点
由于delphi6的TThread destroy函数是这样
if (FThreadID <> 0) and not FFinished then
begin
Terminate;
if FCreateSuspended then
Resume;
WaitFor;
end;
对于 FreeOnTerminate=true的可以这样
try//避免已释放的线程对象再被引用
FThread.Terminate;
while FThread.suspended do
FThread.Resume;//彻底resume
excepte
end
对于 FreeOnTerminate=false的可以这样
FThread.Terminate;
while FThread.suspended do
FThread.Resume;
FThread.Free;
由于delphi6的TThread destroy函数是这样
if (FThreadID <> 0) and not FFinished then
begin
Terminate;
if FCreateSuspended then
Resume;
WaitFor;
end;
对于 FreeOnTerminate=true的可以这样
try//避免已释放的线程对象再被引用
FThread.Terminate;
while FThread.suspended do
FThread.Resume;//彻底resume
excepte
end
对于 FreeOnTerminate=false的可以这样
FThread.Terminate;
while FThread.suspended do
FThread.Resume;
FThread.Free;
#18
如果是在关闭程序前的话
可以直接调用Win32 API函数TerminateThread,更为暴力些
常常这样用的:TerminateThread(myThread.Handle,0);
这样子可以杀掉线程,但是为什么没有释放线程所占用的资源???而且,再次创建的时候,有时候导致系统死锁。
可以直接调用Win32 API函数TerminateThread,更为暴力些
常常这样用的:TerminateThread(myThread.Handle,0);
这样子可以杀掉线程,但是为什么没有释放线程所占用的资源???而且,再次创建的时候,有时候导致系统死锁。
#19
大家继续啊,这么长时间了,呵呵,Delphi的线程这么难用?还是我写的有些问题,大家说得我都试验过了,还没有完全解决。希望大家多多帮忙啊
#20
threada。terminate;
sleep(100);
threada :=nil;
sleep(100);
threada :=nil;
#21
//通知线程终止
FThread.Terminate;
//等待线程退出
while WaitForSingleObject(FThread1.Handle, 0) <> WAIT_OBJECT_0 do
for J := 0 to 1000 do
//如果程序具有Application特性(如EXE),可直接运行下面;否则,如(COM中),请自己重
//写ProcessMessage中的代码
Application.ProcessMessage;
//如果不是FreeOnTerminate的,加上这一句
FThread1.Free;
#22
constructor TMyThread.Create;
begin
inherited Create(false);
FreeOnTerminate:=True;;
end;
begin
inherited Create(false);
FreeOnTerminate:=True;;
end;
#23
上面是自动退出
受控退出:
var
MyThread1:TMyThread;
创建
MyThread1:=TMyThread.Create(false);
释放
MyThread1.Suspend;
MyThread1.Free;
受控退出:
var
MyThread1:TMyThread;
创建
MyThread1:=TMyThread.Create(false);
释放
MyThread1.Suspend;
MyThread1.Free;