Cancel all application from thread in delphi
I have a big problem with my delphi application. I am developing software that uses external security. I am using USB devices that must be connected to the user's computer to run my software. If the user accidentally deletes this key or starts without it, the app should alert the user and stop it immediately. When the app is created, a thread is issued that checks the security device. However, when no checks are performed, such as no device found, this thread does not kill my application. I am using something like this:
retCode := checkSecurity();
if retCode = -1 then
begin
ShowMessage('Security device not found! Terminating immediately!');
Application.Terminate;
end;
The main problem is that it Application.Terminate
doesn't really kill the application. I read in SO and elsewhere that Terminate sends a graceful shutdown signal and waits for other threads, in my case the main application thread, to complete. I really need to kill the application as mentioned, killing all threads and, if possible, exiting, cleaning up to avoid a memory leak, but if not, fine with me. I am using Delphi XE2 developing with Windows 8.1.
Any ideas?
source to share
You can call ExitProcess
to force immediate termination. Compared to the Halt
function, the OS ExitProcess
does even less cleanup.
source to share
To close an application from a stream, you can use:
TThread.Queue(nil,
procedure
begin
Application.Terminate;
end
);
What is cross-platform and almost "the same" as
PostThreadMessage(MainThreadID, WM_QUIT, 0, 0)
given that the application object has not yet been destroyed and you are using Windows.
source to share