user382591
user382591

Reputation: 1390

Thread not terminated while application is terminated under Delphi

assume I have a thread which is still running when the application is terminating

(This thread can not terminate because it waits for a Windows api call to return and that can be long...)

What happens to the thread if the application is closed ?

Can it raise an exception (I'm under Delphi) ?

Upvotes: 2

Views: 1978

Answers (1)

David Heffernan
David Heffernan

Reputation: 612854

I'd say that an exception is very plausible. When you call Application.Terminate this will lead to the following sequence of events:

  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.

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.

Upvotes: 7

Related Questions