Board index » cppbuilder » Cancel button

Cancel button


2005-05-06 02:23:22 AM
cppbuilder9
Hello,
I am looking for a way to put a cancel button in my application so that
when it runs, the user can click on the cancel button (anytime) to stop
the process. Thanks.
Regards,
Claude
 
 

Re:Cancel button

"Claude Tadonki" < XXXX@XXXXX.COM >wrote in message
Quote
I am looking for a way to put a cancel button in my application
so that when it runs, the user can click on the cancel button
(anytime) to stop the process.
Well, first off, it depends on what your process is actually doing to begin
with. Typically, you have two options:
1) have a variable somewhere that both your button and your process can
access. Have the button set the variable value, and have the process check
the value periodically. Assuming your process is running in the main
thread, it will also have to call Application->ProcessMessages() before
checking the variable value, so that the button click has a change to be
processed.
2) move the process code into a worker thread. Then the button can simple
terminate the thread when clicked.
Gambit
 

Re:Cancel button

Remy Lebeau (TeamB) wrote:
Quote

"Claude Tadonki" < XXXX@XXXXX.COM >wrote in message
news:427a649a$ XXXX@XXXXX.COM ...

>I am looking for a way to put a cancel button in my application
>so that when it runs, the user can click on the cancel button
>(anytime) to stop the process.

Well, first off, it depends on what your process is actually doing to
begin with. Typically, you have two options:

1) have a variable somewhere that both your button and your process
can access. Have the button set the variable value, and have the
process check the value periodically. Assuming your process is
running in the main thread, it will also have to call
Application->ProcessMessages() before checking the variable value, so
that the button click has a change to be processed.

2) move the process code into a worker thread. Then the button can
simple terminate the thread when clicked.

In practice, the second form is best implemented as a variation of the
first (eg the worker thread checks a flag, and ...) Calling
TerminateThread(), for example, is not a good way to kill a thread: it
prevents certain resources used by the thread from being cleaned up.
That does not go down well for most programs, particularly long running
ones.
 

{smallsort}