Mega Code Archive

 
Categories / Delphi / Examples
 

Avoiding application processmessages

Q: I am writing a procedure, which may take a long time to execute. I want the user to be able to stop it at any point of execution. I can use Application.ProcessMessages to handle a click of the 'STOP' button. I also thougt about creating a new thread for the procedure. In either case I have to check for a variable 'ProcedureStopped' (with Application.ProcessMessages) or I have to check for MyThread.Terminated. Both of these require frequent polling. I'd prefer some kind of interrupt to exit the procedure. A: By Hallvard Vassbotn, hallvard@millionhandshakes.com : Unfortunately, by default the VCL does not have a very thread-friendly mainthread message loop. It is possible to override the message loop by hooking Application.OnIdle and call MsgWaitForMultipleObjects there. Doing this you can build your own multithread aware message loop and thus easiliy get notified of events and other signals raised from secondary threads.