Board index » delphi » How to stop ongoing calculation process ?

How to stop ongoing calculation process ?

I have a simple form with two buttons on it. Clicking the first button
starts a lengthy calculation process. With the second button I want to stop
and close the form. However, when the calculation process is still ongoing,
the code attached to the stop button is only executed after the first
process is completed (which is also correct but not what I want). I can also
not move the form while the calculation is ongoing.

I am sure this is a common problem but I could not find an easy sample which
handles such a situation. Do I nead to program threads to get the above
example to work, or can I put "control yields" in the calculation process
which allows handling of other tasks ?

Thanks,

Martien

 

Re:How to stop ongoing calculation process ?


Quote
Martien Janssen (reg...@wxs.nl) wrote:

: I have a simple form with two buttons on it. Clicking the first button
: starts a lengthy calculation process. With the second button I want to stop
: and close the form. However, when the calculation process is still ongoing,
: the code attached to the stop button is only executed after the first
: process is completed (which is also correct but not what I want). I can also
: not move the form while the calculation is ongoing.

I would think what you need to do is execute your lengthy calculation
process in another thread.  There was a good example of this in
a back issue of Visual Developer Magazine.
--
Rick Carter
carte...@email.uc.edu

Re:How to stop ongoing calculation process ?


: Martien Janssen <reg...@wxs.nl> schrieb in im Newsbeitrag:
81jth9$fu9...@reader2.wxs.nl...
: I have a simple form with two buttons on it. Clicking the first button
: starts a lengthy calculation process. With the second button I want to
stop
: and close the form. However, when the calculation process is still
ongoing,
: the code attached to the stop button is only executed after the first
: process is completed (which is also correct but not what I want). I can
also
: not move the form while the calculation is ongoing.
:
: I am sure this is a common problem but I could not find an easy sample
which
: handles such a situation. Do I nead to program threads to get the above
: example to work, or can I put "control yields" in the calculation process
: which allows handling of other tasks ?
:
: Thanks,
:
: Martien
:

What kind of calculation is it?

Is it something in a loop?
If yes you could add a new form-wide condition to the loop that can be set
to false outside the loop....
Sorry, don't know how to say it in English...

An example:

type
    TForm1...
    {...}
public
    CalcRun:boolean;
...
end;

procedure TForm1.Button1Click(Sender);
begin
    CalcRun:=true;
    while CalcRun and ({whatever will be done too}) do begin
        {Do the calculation here}
        Application.ProcessMessages; {don't forget this!}
    end;
end;

procedure TForm1.Button2Click(Sender);
begin
    CalcRun:=false;
end;

So the calculation will run until it's execution condition will turn to
false (it has finished) or CalcRun is set to false.
This can be done with Button2.

If I got you right the only problem you have can be solved by putting the
"Application.ProcessMessages" line inside the loop of the calculation...
Hth,
Matthias.

Re:How to stop ongoing calculation process ?


Dear Matthias,

Many thanks for your suggestion. I tried your solution and it does exactly
what I want. In fact I could not find a way in Delphi to yield control to
the operating system (which is performed by the Application.ProcessMessages
instruction). I was afraid that I had to start programming different threads
but your solution is much better for me.

Thanks again,

Martien

Other Threads