Discussion:
[Gambas-user] How to interrupt a loop ?
serge bouc
2006-09-28 12:02:47 UTC
Permalink
Hello everybody,

My problem is now the following : I have a form in Gambas,
with a button called "compute". When this button is clicked,
the program executes "compute_click()", which is essentially
a loop where a computation is done, which may be rather long,
depending on some other parameters of the program.

So I would like to have the possibility of interrupting this computation,
with a click, or a keypress, or whatever. But it seems that once
the computation loop has started, all the events I could use
are inactive (the mouse clicks are no longer seen, nor the keys
pressed, even the timer events), until the loop has terminated.

I imagine I am missing something here. I have tried to define
a new EVENT I could RAISE at some point in the loop, but
I didn't succeed, and I confess I don't understand Gambas
documentation on this subject.

So the only thing I can do at the moment is stop the program by
closing its window, and waiting for KDE to kill the application.
Of course, this is definitely not a good solution... :-(

Thanks for any suggestion.
Serge.
c***@linux.it
2006-09-28 12:14:33 UTC
Permalink
hi
if i've understood the question,
try this way..
-------------
private blnStop as boolean

public sub btnStopLoop_click()
blnStop=true
end sub

public sub Compute()
if blnStop=true then return

compute code

end sub
-------------
bye :)

comisat
Post by serge bouc
Hello everybody,
My problem is now the following : I have a form in Gambas,
with a button called "compute". When this button is clicked,
the program executes "compute_click()", which is essentially
a loop where a computation is done, which may be rather long,
depending on some other parameters of the program.
So I would like to have the possibility of interrupting this computation,
with a click, or a keypress, or whatever. But it seems that once
the computation loop has started, all the events I could use
are inactive (the mouse clicks are no longer seen, nor the keys
pressed, even the timer events), until the loop has terminated.
I imagine I am missing something here. I have tried to define
a new EVENT I could RAISE at some point in the loop, but
I didn't succeed, and I confess I don't understand Gambas
documentation on this subject.
So the only thing I can do at the moment is stop the program by
closing its window, and waiting for KDE to kill the application.
Of course, this is definitely not a good solution... :-(
Thanks for any suggestion.
Serge.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share
your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
_________________________________________________

* GPG/PGP public key-id: 0xB703A9E2
* http://comisat-games.sourceforge.net
* rfc.altervista.org aspetta il tuo contributo
* USE=-brain emerge win
* "Sulla confezione c'era scritto: necessita' di
win9x, winXP o superiore..
...cosi' ho installato linux"
_________________________________________________
nando
2006-09-28 12:21:13 UTC
Permalink
I had the same problem...here's how I did it

I wanted as much CPU for the processing
minimize event processing
and only to test the STOP button occasionally

(example may not sytatically correct)


1) Have a global boolean
2) Have a click button to 'stop'
3) Inside your computations Increment an integer
and every million increments perform a WAIT
4) The WAIT will let the interpreter catch events
and if the stop button was pressed ACTIVE is set TRUE
5) somewhere else in you computations look for ACTIVE = TRUE to quit.

-Fernando




DIM active AS BOOLEAN 'this is a global var
'gets set TRUE when the click button pressed


- - - - - - -

SUB STOP_CLICKBUTTON()

active = TRUE

END sub

- - - - - - - - - -

sub compute()

dim i as integer


' you want to increment i often inside inner loops


'at some part of the code loops that is executed less frequently
(like after NEXT statements)

IF i > 1000000 THEN 'only when i reached 1 million (pick a number you like)

i=0
WAIT 0.1 'this will see if STOP is being pressed

IF ACTIVE = TRUE then GOTO abort_mission 'I used a goto in some programs or BREAK

ENDIF


abort_mission:
end sub


- - - - - - -




---------- Original Message -----------
From: serge bouc <***@free.fr>
To: gambas-***@lists.sourceforge.net
Sent: Thu, 28 Sep 2006 14:02:47 +0200
Subject: [Gambas-user] How to interrupt a loop ?
Post by serge bouc
Hello everybody,
My problem is now the following : I have a form in Gambas,
with a button called "compute". When this button is clicked,
the program executes "compute_click()", which is essentially
a loop where a computation is done, which may be rather long,
depending on some other parameters of the program.
So I would like to have the possibility of interrupting this computation,
with a click, or a keypress, or whatever. But it seems that once
the computation loop has started, all the events I could use
are inactive (the mouse clicks are no longer seen, nor the keys
pressed, even the timer events), until the loop has terminated.
I imagine I am missing something here. I have tried to define
a new EVENT I could RAISE at some point in the loop, but
I didn't succeed, and I confess I don't understand Gambas
documentation on this subject.
So the only thing I can do at the moment is stop the program by
closing its window, and waiting for KDE to kill the application.
Of course, this is definitely not a good solution... :-(
Thanks for any suggestion.
Serge.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------
serge bouc
2006-09-28 15:38:02 UTC
Permalink
Post by c***@linux.it
hi
if i've understood the question,
try this way..
-------------
private blnStop as boolean
public sub btnStopLoop_click()
blnStop=true
end sub
public sub Compute()
if blnStop=true then return
compute code
end sub
-------------
bye :)
comisat
Thanks for the idea. It does'nt work, since the
click event on "btnStopLoop" is not recognized while
inside the compute loop. So nothing happens when
I click on the stop button during the computation.
Thanks anyway.
Serge.
serge bouc
2006-09-28 15:54:21 UTC
Permalink
Post by nando
I had the same problem...here's how I did it
I wanted as much CPU for the processing
minimize event processing
and only to test the STOP button occasionally
(example may not sytatically correct)
1) Have a global boolean
2) Have a click button to 'stop'
3) Inside your computations Increment an integer
and every million increments perform a WAIT
4) The WAIT will let the interpreter catch events
and if the stop button was pressed ACTIVE is set TRUE
5) somewhere else in you computations look for ACTIVE = TRUE to quit.
-Fernando
It works ! The key was to use "WAIT" to catch events...
(once again, the Gambas documentation is rather obscure
on this subject...)
Many thanks for this idea !
Serge.
c***@linux.it
2006-09-28 16:01:13 UTC
Permalink
i'm sorry, i supposed you let events in your compute code with WAIT
The documentation is clear on it
WAIT without delay let events (think DoEvents on VB)
WAIT with delay get a pause (think sleep call on VB)

comisat
Post by serge bouc
Post by c***@linux.it
comisat
Thanks for the idea. It does'nt work, since the
click event on "btnStopLoop" is not recognized while
inside the compute loop.
_________________________________________________

* GPG/PGP public key-id: 0xB703A9E2
* http://comisat-games.sourceforge.net
* rfc.altervista.org aspetta il tuo contributo
* USE=-brain emerge win
* "Sulla confezione c'era scritto: necessita' di
win9x, winXP o superiore..
...cosi' ho installato linux"
_________________________________________________
serge bouc
2006-09-28 19:55:28 UTC
Permalink
Post by c***@linux.it
i'm sorry, i supposed you let events in your compute code with WAIT
The documentation is clear on it
WAIT without delay let events (think DoEvents on VB)
WAIT with delay get a pause (think sleep call on VB)
comisat
WAIT [delay] : Calls the event loop. If Delay is specified, does not
return until Delay seconds elapse.
Delay is a floating point number. So, if you want to wait 100 ms, just
WAIT 0.1
During the wait, no keyboard or mouse events are processed. Only
drawing, timer and file descriptors events, like Process_Write.
This is not so clear to me : apparently, you suggest that I should put a
"WAIT" *without* delay
in the loop I want to interrupt ? But I tried that, and it doesn't seem
to work. The result is the same
as with no "WAIT" at all. If I put a "WAIT 0.01" on the contrary, then
the events (mouse clicks) *are*
recognized.
Probably what happens is that I don't understand enough of computer
programmers language
to see how this behaviour is explained by the above documentation. Once
again, I am far from an expert.

Best regards.
Serge.
Benoit Minisini
2006-09-28 20:06:45 UTC
Permalink
Post by serge bouc
Post by c***@linux.it
i'm sorry, i supposed you let events in your compute code with WAIT
The documentation is clear on it
WAIT without delay let events (think DoEvents on VB)
WAIT with delay get a pause (think sleep call on VB)
comisat
WAIT [delay] : Calls the event loop. If Delay is specified, does not
return until Delay seconds elapse.
Delay is a floating point number. So, if you want to wait 100 ms, just
WAIT 0.1
During the wait, no keyboard or mouse events are processed. Only
drawing, timer and file descriptors events, like Process_Write.
This is not so clear to me : apparently, you suggest that I should put a
"WAIT" *without* delay
in the loop I want to interrupt ? But I tried that, and it doesn't seem
to work. The result is the same
as with no "WAIT" at all. If I put a "WAIT 0.01" on the contrary, then
the events (mouse clicks) *are*
recognized.
Probably what happens is that I don't understand enough of computer
programmers language
to see how this behaviour is explained by the above documentation. Once
again, I am far from an expert.
Best regards.
Serge.
That's it.

The documentation is right there. Sometimes it is. :-)

You have a little information on event loop there:
http://gambasdoc.org/help/cat/eventloop

Regards,
--
Benoit Minisini
serge bouc
2006-09-29 15:01:44 UTC
Permalink
Post by Benoit Minisini
That's it.
The documentation is right there. Sometimes it is. :-)
http://gambasdoc.org/help/cat/eventloop
Regards,
Thanks for the link. I find the explanation on "wait" there much more clear
than what I read before (from the documentation coming with gambas distribution).
Post by Benoit Minisini
Post by c***@linux.it
WAIT [delay] : Calls the event loop. If Delay is specified, does not
return until Delay seconds elapse.
Delay is a floating point number. So, if you want to wait 100 ms, just
WAIT 0.1
During the wait, no keyboard or mouse events are processed. Only
drawing, timer and file descriptors events, like Process_Write.
*
*
WAIT [ _Delay_ ]
Calls recursively the event loop
If _Delay_ is specified, the function does not return until _Delay_
seconds elapse.
If _Delay_ is not specified, the functions processes every events and
returns immediately.
In this case, keyboard and mouse events are ignored.
Example
**' **Waits a little, letting the user interacts with the GUI**
**WAIT 0.1**
' **Waits, but the user can just watch what happens...**
**WAIT**
**
The main differences that help me to understand this version are
the words "In this case..." (when delay is not specified), and
of course the example.

Thanks again.
Serge.
*
*





-- Benoit Minisini

Loading...