Use a worker TThread so that the code won't block the user interface.
I think I figured-out what you meant... and I got the attached small test project working.
I would be really grateful if you take a look at the code/logic, and see it I've done anything wrong.
I'm a bit dubious about the way I'm sharing a global variable between the two threads/units; although I am careful to make sure that both cannot write at the same time.
I want to make sure I get this right, because I've used the same logic/method in my Checkers program (discussed in a nearby thread).
Thanks.
PS - if you run the program, it waits for a mouse click, then it sets the thread running which waits for the QUIT variable to be set, then the main loop waits until it receives an [ESC] key press, then sets the QUIT variable and then waits for the thread to finish.
PS - this has bugs - see below