问题描述
假定我有一个线程在应用程序终止时仍在运行
assume I have a thread which is still running when the application is terminating
(此线程无法终止,因为它等待Windows api调用返回可能会很长...)
(This thread can not terminate because it waits for a Windows api call to returnand that can be long...)
如果应用程序关闭,线程会怎样?
What happens to the thread if the application is closed ?
会引发例外情况吗(我在Delphi的监督下)?
Can it raise an exception (I'm under Delphi) ?
推荐答案
我想说一个异常是很合理的.当您调用 Application.Terminate
时,将导致以下事件序列:
I'd say that an exception is very plausible. When you call Application.Terminate
this will lead to the following sequence of events:
- 对
PostQuitMessage
的调用. -
Application.Terminated
被设置为True
. -
Application.Run
返回. -
System.Halt
被调用. 将运行 - 退出过程,特别是
DoneApplication
,这将拆除Application
及其拥有的所有组件.嗯,希望您的线程不会访问Application
所拥有的任何内容. -
FinalizeUnits
被调用.哦,哦.内存管理器已关闭,此外还有更多. 调用 -
ExitProcess
.现在您的线程已被杀死.
- A call to
PostQuitMessage
. Application.Terminated
being set toTrue
.Application.Run
returning.System.Halt
is called.- Exit procedures are run, specifically
DoneApplication
which will tear downApplication
and all components that it owns. Hmm, better hope your thread does not access anything owned byApplication
. FinalizeUnits
is called. Uh-oh. Memory manager is shut down, and lots more beside.ExitProcess
is called. Now your thread is killed.
您的线程将继续运行,直到调用 ExitProcess
.如果它执行的代码完全不受调用 DoneApplication
和 FinalizeUnits
的影响,那么您应该会遇到问题.
Your thread will carry on running until the call to ExitProcess
. If it executes any code at all that would be affected by the calls to DoneApplication
and FinalizeUnits
, then you should expect problems.
这篇关于在Delphi下终止应用程序时,线程未终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!