2
votes

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) ?

1
Well, how does your application terminate? There's more than one way to do it.David Heffernan
application.terminate;user382591
Which Win32 API function is your worker thread actually using? Chances are that it provides a way of canceling the function so your worker thread can terminate itself properly when requested. The main thread should be terminating the worker thread before exiting the process.Remy Lebeau

1 Answers

7
votes

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.