线程中的奇怪问题,应用程序无法退出 [英] Strange problem in threads, application doesn't exit
问题描述
好的,我正在编写一个Zoom组件,并且希望它从辅助线程的屏幕上捕获。
您可以假装我只想在一个空组件中派生TThread。我没有在线程中编写任何代码,所以它只是一个简单的无用线程。我编写了这段代码: Thrd:= TCaptureThread.Create(False);
在组件主类中。
然后我在主类销毁代码中编写了 Thrd.Free
。现在,当我关闭整个应用程序时,尽管它破坏了所有内容,但该过程并未完全终止。在Windows中,任务管理器显示线程数为1,但进程仍然存在。如果我在线程创建行中添加注释,则一切正常,应用程序快速终止。我要怎么办? :(
OK I'm writing a Zoom component and I wanted it to capture from screen within a secondary thread.
You can pretend I just want to derive TThread in an empty component. I didn't write any codes in the thread so it is just a simple useless thread. I wrote this code: Thrd := TCaptureThread.Create(False);
in the component main class.
Then I wrote Thrd.Free
in the main class destruction code. Now when I close the whole application, although it destroys everything, the process doesn't terminate completely. In Windows Task Manager shows that number of threads is 1 but the process remains. If I comment the thread creation line, everything becomes OK and application terminates quickly. What am I gonna do about this? :(
预先感谢
推荐答案
当您致电 Thrd.Free
运行来自 TThread.Destroy
的以下代码:
When you call Thrd.Free
the following code from TThread.Destroy
is run:
Terminate;
if FCreateSuspended then
Resume;
WaitFor;
在线程上调用 Free
将因此同步终止线程。
Calling Free
on a thread will thus terminate the thread synchronously.
我的猜测是对 WaitFor
的调用永远不会返回。也许 TCaptureThread.Execute
不会检查已终止
并退出,也许 TCaptureThread
正在等待主线程,因此等待线程死锁。
My guess is that the call to WaitFor
never returns. Perhaps TCaptureThread.Execute
doesn't check Terminated
and exit. Perhaps TCaptureThread
is waiting on the main thread and so the wait on the thread deadlocks.
根据您的问题,除了猜测外,很难做其他任何事情,但是我想检查您的代码是否超过了 WaitFor
调用销毁 Thrd
启用调试DCU,将一个断点设置为对 WaitFor
的调用你自己。
It's pretty hard to do anything other than guess, based on your question, but I'd want to check whether or not your code gets past the WaitFor
call in the destruction of Thrd
. Enable Debug DCUs, set a breakpoint one the call to WaitFor
and see for yourself.
这篇关于线程中的奇怪问题,应用程序无法退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!