Board index » delphi » Stop processing...

Stop processing...

  I have quite a few procedures that process data within
a program.  I have Application.ProcessMessages within
these procedures to allow the clicking of a button on
the main form.  This main form button needs to stop any data
processing when it is clicked.  Is there any instruction
that can be executed to stop the program from going any
further than the Stop button procedure, thus terninating
processing that way going on in the current data processing
routine?  Something like 'Halt', only halt terminates the
program altogether.  Possibly an assembler routine that
will send the program back to idle state.  Now the only
thing I can figure is to open a non existent file and
capture the error in my own error routine.  This does
treminate the procssing.

  Thanks

 

Re:Stop processing...


On the stop button, just set a boolean variable like stopit. Then in your
loop, check to see if stopit is set, if it is, break from the procedure.

Nick

Quote
"GCS" <battleDELETET...@netexecutive.com> wrote in message

news:Xns921B86B6C483GCS@207.218.245.68...
Quote
>   I have quite a few procedures that process data within
> a program.  I have Application.ProcessMessages within
> these procedures to allow the clicking of a button on
> the main form.  This main form button needs to stop any data
> processing when it is clicked.  Is there any instruction
> that can be executed to stop the program from going any
> further than the Stop button procedure, thus terninating
> processing that way going on in the current data processing
> routine?  Something like 'Halt', only halt terminates the
> program altogether.  Possibly an assembler routine that
> will send the program back to idle state.  Now the only
> thing I can figure is to open a non existent file and
> capture the error in my own error routine.  This does
> treminate the procssing.

>   Thanks

Re:Stop processing...


"GCS" <battleDELETET...@netexecutive.com> skrev i melding
news:Xns921B86B6C483GCS@207.218.245.68...

Quote
>   I have quite a few procedures that process data within
> a program.  I have Application.ProcessMessages within
> these procedures to allow the clicking of a button on
> the main form.  This main form button needs to stop any data
> processing when it is clicked.  Is there any instruction
> that can be executed to stop the program from going any
> further than the Stop button procedure, thus terninating
> processing that way going on in the current data processing
> routine?  Something like 'Halt', only halt terminates the
> program altogether.  Possibly an assembler routine that
> will send the program back to idle state.  Now the only
> thing I can figure is to open a non existent file and
> capture the error in my own error routine.  This does
> treminate the procssing.

Abort;

--
Bj?rge S?ther
bjorge@hahaha_itte.no

Re:Stop processing...


Im Artikel <Xns921B86B6C483...@207.218.245.68>, GCS
<battleDELETET...@netexecutive.com> schreibt:

Quote
>Now the only
>thing I can figure is to open a non existent file and
>capture the error in my own error routine.

This is one way, when e.g. the currently processed file is closed from the
button click.

A less rude procedure uses a variable, which is set (or cleared) by the click,
and which is frequently tested in the executing code. When your code contains
loops, or frequently calls the same subroutine(s), then you can place the tests
there.

Both methods require that some condition actually is tested in the running
code, maybe implicitly by reading or writing an file, which will produce an
error when the file is closed, or explicitly by testing an semaphore. When your
code should stop only at certain points, when some indivisible operation is
finished, then you should place the tests just there.

DoDi

Re:Stop processing...


Quote
"GCS" <battleDELETET...@netexecutive.com> wrote in message

news:Xns921B86B6C483GCS@207.218.245.68...

Quote
>   I have quite a few procedures that process data within
> a program.  I have Application.ProcessMessages within
> these procedures to allow the clicking of a button on
> the main form.  This main form button needs to stop any data
> processing when it is clicked.  Is there any instruction
> that can be executed to stop the program from going any
> further than the Stop button procedure, thus terninating
> processing that way going on in the current data processing
> routine?  Something like 'Halt', only halt terminates the
> program altogether.  Possibly an assembler routine that
> will send the program back to idle state.  Now the only
> thing I can figure is to open a non existent file and
> capture the error in my own error routine.  This does
> treminate the procssing.

processing in a thread is one stack, the stop click cannot
_easily_ unwind the stack frame and return to the
message handler
 a try.. except in the dispatcher will do it..
  except using try is for guarding resources
   not making code easier

unwinding a stack can cause problems with non-stack resources
allocated by the procedure, they go to limbo

You can however arrange to stop any further processing .

the stop click actually runs during application.processmessages
a semaphore set during the stop click can be the first thing
done after the click stop click set it  ( assuming no other handlers
also run  before application.porcessmessages returns )

a few ways here. many more are possible

ButtonZ looks like more work, but you likely have
a  try..except structure somewhere in a process you
want to be abortable.

type EStopException = class(Exception);
type ECrashBackToDispatcherException = class(Exception);

var stopprocessing:boolean=false;

procedure ProcessMessagesWithExcept;
begin
  Application.ProcessMessages;
  if stopprocessing then
  begin
    stopprocessing:=false;
    raise EStopException.create('');
  end;
end;

procedure ProcessMessagesWithAbort;
begin
  Application.ProcessMessages;
  if stopprocessing then
  begin
    stopprocessing:=false;
    Abort;
  end;
end;

// the usual way, straightforward
procedure TForm1.ButtonXClick(Sender: TObject);
begin
     stopprocessing:=false;
     //init code
     repeat
      ...
      Application.ProcessMessages;
      if stopprocessing then break; // or exit or abort
     until false;
     // cleanup code
end;

procedure TForm1.ButtonYClick(Sender: TObject);
begin
    // init code
     repeat
       ...
       ProcessMessagesWithAbort;
     until false;
    // cleanup code, never run , dangle-de-handle
end;

procedure TForm1.ButtonZClick(Sender: TObject);
   begin
  //init code
  try
    repeat
      ...
      Application.processmessages; // where it is unsafe
       ...
       ProcessMessagesWithExcept;// where safe to stop
     until false;
  except
    on EstopException do ;
  end;
  //cleanup code
end;

procedure TForm1.StopButtonClick(Sender: TObject);
begin
 if stopprocessing
 then
     beep
 else
    stopprocessing:=true;
end;

Other Threads