Forum > LCL

Cancel a procedure from another procedure

(1/2) > >>

Waco:
Hi, basically I'd like to be able to quit my application at any time. However, when a procedure is being executed, whether I press the X or my button with Application.Terminate, program first finishes the procedure and then immediatelly quits. In fact, even pausing the procedure doesn't help, I still can't quit my program. I don't have very clear idea of how to solve it, so I hope you could propose something, please.

theo:
You could put the lengty operation in a separate thread or make the procedure call
Application.Processmessages periodically.

Waco:
When starting this application I though I'd have to use more threads, but Application.ProcessMessages did the trick everywhere I needed, so I'm still trying to avoid it. It looks pretty complicated for a beginner like me...

If by calling Application.ProcessMessages periodically you meant something like this

--- Code: ---procedure TForm1.ButtonClick(Sender: TObject);
begin
  while i<>0 do begin //this condition is always fulfilled
    Application.Terminate;
    Application.ProcessMessages;
  end;
end;
--- End code ---
then it didn't work, I wasn't able to quit even after the other procedure finished.

I should probably add that when I'm trying to quit the first procedure is trapped in Sleep() and also calling Application.ProcessMessages every 10 ms. Might that matter?

theo:
I don't really understand what you're doing.

But it seems that you don't understand a basic thing:
A single threaded application can only do one thing at a time.
If it's in a lengthy procedure, it does nothing else!!!
It does not react on events like clicks etc. until the procedure has finished.

If you want it to "listen" to events like button click, you have to call Application.Processmessages periodically inside your lenghty operation, not anywhere else.
Sleep ist just blocking the whole application, it does NOTHING while it sleeps, and continues after that where it was before.

Waco:
It might very well be true that I don't understand some things, I'm just a beginner getting some experience. But I get everything you wrote and I think I knew all of that before. So I'll try to explain my problem again, in detail.

I have this lengthy procedure (let's call it that way), which regularly calls another short procedure. This one includes just a loop with Sleep(10) and Application.ProcessMessages which is repeated based on the argument. GUI normally reacts to every action I do except for my attempts to quit, either by the X button or my button with Application.ProcessMessages, like I said. So I really don't get it. The lengthy procedure 'gives the CPU' to the short one, which then enables any other executed action to be noticed and immediately performed. Am I wrong in something?

Navigation

[0] Message Index

[#] Next page

Go to full version