Raoni Medinilha
Raoni Medinilha

Reputation: 57

Abort entire application from thread in delphi

I have a major problem with my delphi App. I'm developing a software that uses external security. I'm using usb devices that must be connected to the users machine in order for my software to run. If by any chance the user removes this dongle, or starts without it, the app is supposed to warn the user and stop immediately. A thread is being released at the creation of the app, which checks for the security device. However, when the checks fail, e.g. no device found, that thread is not killing my app. I'm using something like this:

    retCode := checkSecurity();
    if retCode = -1 then
    begin
        ShowMessage('Security device not found! Terminating immediately!');
        Application.Terminate;
    end;

The major problem here is that Application.Terminate doesn't really kill the app. I've read on SO and other places that the Terminate sends a signal for a gracefull shutdown and waits for other threads, in my case the main thread of the app, to finish. I really need to kill the app as mentioned, killing all threads and exiting, if possible, cleaning up to avoid memory leakage, but if not, fine with me. I'm using Delphi XE2, developing with Windows 8.1.

Any ideas?

Upvotes: 3

Views: 1863

Answers (3)

iPath ツ
iPath ツ

Reputation: 2488

To terminate application from a thread you may use:

TThread.Queue(nil, 
  procedure
  begin
    Application.Terminate;
  end
);

Which is cross-platform and is "almost" the same as

PostThreadMessage(MainThreadID, WM_QUIT, 0, 0)

given that the Application object was not destroyed already and you use Windows.

Upvotes: 2

gabr
gabr

Reputation: 26850

To kill an app, just call Halt. It's a leftover from the DOS times, adapted to work nicely with modern Delphi apps. It will call all 'finalization' blocks in all units but not much more.

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 613511

You can call ExitProcess to force immediate termination. In comparison to Halt, the OS function ExitProcess performs even less clean up.

Upvotes: 4

Related Questions