How do you cleanly abort a Delphi program?

前端 未结 5 1616
温柔的废话
温柔的废话 2020-12-28 08:15

I\'ve got a program that\'s having some trouble during shutdown, raising exceptions that I can\'t trace back to their source. It appears to be timing-related and non-determ

相关标签:
5条回答
  • 2020-12-28 08:37

    In case HeartWare's suggestion of using ExitProcess() fails, it might be you are using some DLL's that do not respond well to the DLL_PROCESS_DETACH. In that case, try using a TerminateProcess( getCurrentProcess, 0 );

    Once you resort to such measures, one might wonder if the "cleanly" part of the topic title still holds up to scrutiny.

    0 讨论(0)
  • 2020-12-28 08:49

    Halt(0) used to be the good old fashioned way of telling the program to end with immediate effect. There's probably a more Delphi-friendly way of doing that now, but I'm 95% sure halt(0) still works. :-)

    0 讨论(0)
  • 2020-12-28 08:51

    ExitProcess(0) ?

    0 讨论(0)
  • 2020-12-28 08:54

    The last time I had to hunt a problem like this was the shutdown was a causing an event (resize? It's been a while.) to fire on the dying window causing an attempt to redraw something that needed stuff that had already been disposed of.

    0 讨论(0)
  • 2020-12-28 08:57

    After looking at the Delphi Run Time Library source code, and at the Microsoft documentation; I can corroborate Mason and Paul-Jan comments.

    The hierarchy of shutdown is as follows

      Application.Terminate()
        performs some unidentified housekeeping of application
        calls Halt()
    
      Halt()
        calls ExitProc if set
        alerts the user in case of runtime error
        get rid of PackageLoad call contexts that might be pending
        finalize all units
        clear all exception handlers
        call ExitprocessProc if set
        and finally, call ExitProcess() from 'kernel32.dll'
    
      ExitProcess() 
        unloads all DLLs
        uses TerminateProcess() to kill the process
    
    0 讨论(0)
提交回复
热议问题