本文介绍了在Delphi下终止应用程序时,线程未终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定我有一个线程在应用程序终止时仍在运行

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:

  1. PostQuitMessage 的调用.
  2. Application.Terminated 被设置为 True .
  3. Application.Run 返回.
  4. System.Halt 被调用.
  5. 将运行
  6. 退出过程,特别是 DoneApplication ,这将拆除 Application 及其拥有的所有组件.嗯,希望您的线程不会访问 Application 所拥有的任何内容.
  7. FinalizeUnits 被调用.哦,哦.内存管理器已关闭,此外还有更多.
  8. 调用
  9. ExitProcess .现在您的线程已被杀死.
  1. A call to PostQuitMessage.
  2. Application.Terminated being set to True.
  3. Application.Run returning.
  4. System.Halt is called.
  5. Exit procedures are run, specifically DoneApplication which will tear down Application and all components that it owns. Hmm, better hope your thread does not access anything owned by Application.
  6. FinalizeUnits is called. Uh-oh. Memory manager is shut down, and lots more beside.
  7. 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下终止应用程序时,线程未终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:58