Board index » delphi » How to interupt(stop) the process in Delphi

How to interupt(stop) the process in Delphi

Hi !

  Here is my problem:
  I need to stop the program while he is running. For example let's take
the loop (for,repeat-until or while) . The loop must stop when the user
presses "Esc" or choose "Cancel" from the menu.
 In pascal I can do something like this:

repeat
   ...
until  KeyPressed;

but what can I do in Delphi. How can I stop any process by just pressing
"Esc".
Please help me! Thank You.

Leon

 

Re:How to interupt(stop) the process in Delphi


 Hi.
In a multi tasking environment, you need to put your loop in a separate
thread or your process will take up all the cycles. So put your loop in a
thread like this:

PROCEDURE Loop.Execute;
BEGIN
   REPEAT
   ....
   UNTIL Terminated;
END;

And in your main thread, use the OnKeyDown method to set terminate if a user
presses esc.

Regards
Stig Johansen
DATAgruppen Danmark A/S

Simon Sander wrote in article <34303EA1.D1C53...@uni-mb.si>...

Quote
>Hi !

>  Here is my problem:
>  I need to stop the program while he is running. For example let's take
>the loop (for,repeat-until or while) . The loop must stop when the user
>presses "Esc" or choose "Cancel" from the menu.
> In pascal I can do something like this:

>repeat
>   ...
>until  KeyPressed;

>but what can I do in Delphi. How can I stop any process by just pressing
>"Esc".
>Please help me! Thank You.

>Leon

Re:How to interupt(stop) the process in Delphi


In article <34303EA1.D1C53...@uni-mb.si>,

Quote
Simon Sander <simon.san...@uni-mb.si> wrote:
>Hi !

>  Here is my problem:
>  I need to stop the program while he is running. For example let's take
>the loop (for,repeat-until or while) . The loop must stop when the user
>presses "Esc" or choose "Cancel" from the menu.
> In pascal I can do something like this:

>repeat
>   ...
>until  KeyPressed;

>but what can I do in Delphi. How can I stop any process by just pressing
>"Esc".
>Please help me! Thank You.

If you absolutely need to run a loop, you could do something like this:

KeyPressed := false;     //declared as global for the form.
repeat
     ....
     application.processmessages;
until KeyPressed;

Then you need to use the OnKeyDown (or OnKeyUp) event for the form.

procedure TMyForm.but1KeyDown(Sender: TObject; var Key: Word;
     Shift: TShiftState);
begin
     case Key of
     VK_ESC : Keyressed := true;   //I think it's VK_ESC for esc...
     ...  Check for more keys here if needed.
     end;
end;

You should also set the form's KeyPreview to True.

BTW, I believe a more correct way of doing it in WIndows is to use a timer.
Start the timer when the loop should start.
In the OnTimer event you could put the code that should be in the loop.
Stop the timer in the OnKeyDown event.
But this might not be fast enough, as the standard timer resolution is
only 55ms ?

--
|| Bjorn K. Nilssen      // http://home.sn.no/~bjoernk/  // mainly 3D ||

Re:How to interupt(stop) the process in Delphi


 Try adding "application.processmessages;" inside your loop.  This will
cause the program to allow windows to respond to windows messages such as
pushing a key or button.

Simon Sander wrote in article <34303EA1.D1C53...@uni-mb.si>...

Quote
>Hi !

>  Here is my problem:
>  I need to stop the program while he is running. For example let's take
>the loop (for,repeat-until or while) . The loop must stop when the user
>presses "Esc" or choose "Cancel" from the menu.
> In pascal I can do something like this:

>repeat
>   ...
>until  KeyPressed;

>but what can I do in Delphi. How can I stop any process by just pressing
>"Esc".
>Please help me! Thank You.

>Leon

Re:How to interupt(stop) the process in Delphi


        I think the best approach would be using a separate thread for your loop. It
will provide better performance and control, with least impact on other
processes/threads. You can stop it anytime with a Thread.Terminate or signal
the thread to finish through an Windows Object (Event, Mutex, Semaphore) using
WaitSingleObject to detect it.

        Take a look at Delphi Help and Sample on Threads.      
--
Vaner Costa Rilho
RILHO Associados Informtica
rilho...@pro.via-rs.com.br

Bjorn-Kare Nilssen <bjoe...@sn.no> escreveu no artigo
<+TNM0gmCgM/A09...@sn.no>...

Quote
> In article <34303EA1.D1C53...@uni-mb.si>,
> Simon Sander <simon.san...@uni-mb.si> wrote:
> >Hi !

> >  Here is my problem:
> >  I need to stop the program while he is running. For example let's take
> >the loop (for,repeat-until or while) . The loop must stop when the user
> >presses "Esc" or choose "Cancel" from the menu.
> > In pascal I can do something like this:

> >repeat
> >   ...
> >until  KeyPressed;

> >but what can I do in Delphi. How can I stop any process by just pressing
> >"Esc".
> >Please help me! Thank You.

> If you absolutely need to run a loop, you could do something like this:

> KeyPressed := false;     //declared as global for the form.
> repeat
>      ....
>      application.processmessages;
> until KeyPressed;

> Then you need to use the OnKeyDown (or OnKeyUp) event for the form.

> procedure TMyForm.but1KeyDown(Sender: TObject; var Key: Word;
>      Shift: TShiftState);
> begin
>      case Key of
>      VK_ESC : Keyressed := true;   //I think it's VK_ESC for esc...
>      ...  Check for more keys here if needed.
>      end;
> end;

> You should also set the form's KeyPreview to True.

> BTW, I believe a more correct way of doing it in WIndows is to use a timer.
> Start the timer when the loop should start.
> In the OnTimer event you could put the code that should be in the loop.
> Stop the timer in the OnKeyDown event.
> But this might not be fast enough, as the standard timer resolution is
> only 55ms ?

> --
> || Bjorn K. Nilssen      // http://home.sn.no/~bjoernk/  // mainly 3D ||

Other Threads