From: ················@googlemail.com
Subject: Breaking from a loop with a global variable
Date: 
Message-ID: <1178910022.684989.240650@o5g2000hsb.googlegroups.com>
Hi,
I'm trying to do some robotics programming using Common Lisp (SBCL
0.9.16) on Debian Linux. The first problem I ran into was accessing
the serial port, and I solved that one in a rather complicated way
using sockets. Now I need a very simple visual interface on which I
can press a button to pause everything, and continue with another
button press. The easiest GUI solution I could find was LTK (
http://www.peter-herth.de/ltk/ ). But I ran into this really silly
problem; I can't stop a loop using a global variable. The (test) code
is as follows:

;;lisp_loop_break.lisp
(require 'ltk)
(defparameter *stopped* nil)   ;;also tried defvar, but didn't make
any difference,
                                           ;;as far as i know it
shouldn't anyway

(defun pause (millisec)
  (do ((N (get-internal-real-time)))
      ((> (- (get-internal-real-time) N) millisec))) T)


(defun very-simple-window()
  (ltk:with-ltk()
    (let* ((f (make-instance 'ltk:frame))
	  (start-button (make-instance 'ltk:button
			  :master f
			  :text "Do irrelevant stuff"
			  :command 'do-loop))
	  (stop-button (make-instance 'ltk:button
			  :master f
			  :text "Stop"
			  :command 'stop))
	  )
      (ltk:pack f)
      (ltk:pack start-button :side :left)
      (ltk:pack stop-button :side :left)
      )))

(defun stop() (setf *stopped* t))

(defun do-loop()
  (loop for i from 0 to 10 while (not *stopped*) do
	(pause 500)
	(format t "*stopped*: ~A ~%" *stopped*)))

(very-simple-window)
;;end of file

If I load this on SBCL and press the "Do irrelevant stuff" button, it
just prints:
 *stopped*: NIL
10 times, although I keep on pressing the Stop button. I have also
tried making the do-loop recursive instead of looping, but that
doesn't work either. I also tried the same thing on CLisp and the
behavior is the same (which somehow doesn't surprise me).

Sorry if the solution is very obvious or the problem is due to my poor
understanding of something, but I just couldn't figure it out despite
intensive google searches and tries. Also, any recommendations about
visual interfaces with lisp are also highly appreciated.

Cheers,
-Ulas

From: Peter Herth
Subject: Re: Breaking from a loop with a global variable
Date: 
Message-ID: <f22j3i$1jr$03$1@news.t-online.com>
The explanation for your observation is very simple: new events
are only processed after the button callback returns, that is,
after your loop finishes. If you call (process-events) somewhere
in the loop, you can react on button presses while it runs.

Peter

-- 
Ltk, the easy lisp gui http://www.peter-herth.de/ltk/
From: ················@googlemail.com
Subject: Re: Breaking from a loop with a global variable
Date: 
Message-ID: <1178987553.003952.188430@e65g2000hsc.googlegroups.com>
On May 11, 10:18 pm, Peter Herth <·······@t-online.de> wrote:
> The explanation for your observation is very simple: new events
> are only processed after the button callback returns, that is,
> after your loop finishes. If you call (process-events) somewhere
> in the loop, you can react on button presses while it runs.

thanks a lot, that solved it. that was the only thing i oversaw in the
example, i think.

-Ulas