Recent

Author Topic: Cancel a procedure from another procedure  (Read 30969 times)

Waco

  • Jr. Member
  • **
  • Posts: 61
Cancel a procedure from another procedure
« on: January 26, 2010, 04:07:25 pm »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1929
Re: Cancel a procedure from another procedure
« Reply #1 on: January 26, 2010, 04:15:39 pm »
You could put the lengty operation in a separate thread or make the procedure call
Application.Processmessages periodically.

Waco

  • Jr. Member
  • **
  • Posts: 61
Re: Cancel a procedure from another procedure
« Reply #2 on: January 26, 2010, 05:18:00 pm »
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: [Select]
procedure TForm1.ButtonClick(Sender: TObject);
begin
  while i<>0 do begin //this condition is always fulfilled
    Application.Terminate;
    Application.ProcessMessages;
  end;
end;
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?
« Last Edit: January 26, 2010, 05:24:55 pm by Waco »

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1929
Re: Cancel a procedure from another procedure
« Reply #3 on: January 26, 2010, 05:43:16 pm »
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

  • Jr. Member
  • **
  • Posts: 61
Re: Cancel a procedure from another procedure
« Reply #4 on: January 26, 2010, 06:26:16 pm »
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?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1929
Re: Cancel a procedure from another procedure
« Reply #5 on: January 26, 2010, 06:56:34 pm »
I think I understand now ;-)
You should exit the procedure too. Look at this:

Quote
var running:boolean;

implementation

{ TForm1 }

procedure lengthy;
begin
while true do //forever
  begin
    Application.ProcessMessages;
    if not running then break;
    sleep(50);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  running:=true;
  lengthy;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
  running:=false;
end;

Waco

  • Jr. Member
  • **
  • Posts: 61
Re: Cancel a procedure from another procedure
« Reply #6 on: January 26, 2010, 07:22:32 pm »
Thank you very much. That's exactly what I was looking for, especially the CloseQuery, I didn't know about that.

 

TinyPortal © 2005-2018