[Gambas-user] How to interrupt a loop ?

nando nando_f at ...1382...
Thu Sep 28 14:21:13 CEST 2006


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 <serge.bouc at ...402...>
To: gambas-user at lists.sourceforge.net
Sent: Thu, 28 Sep 2006 14:02:47 +0200
Subject: [Gambas-user] How to interrupt a loop ?

> 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
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------





More information about the User mailing list