[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Mr.Ed question



The refreshing behvaior is a little bit platform dependant. When you
call the set-label method, the underlying GUI system may choose to
update the message right then, or it may just queue anupdate event. If
it does queue an update event, your code is starving the update, since
you are issuing a sleep on the event handling thread.

More generally, there is one thread that handles all GUI events (for a
particular but eventspaces are a story for another day -- for now
assume that there is only one eventspace -- then everything I am about
to write is true) in your program. When the user clicks and a callback
is called, or you evealuate something in the REPL, that all happens on
the same thread. In fact, all user input is queue'd in an event queue
and the event handling thread reads items out the queue and processes
them (both for repl interactions and for button callbacks and the
like).

That means that a sleep is a Bad Thing(tm). Instead, either have a
button that the user clicks to move to the next question, or use this
technique:

(define right-answer
  (lambda ()
    (begin
      (send message1 set-label "You're right!") ;message% object
      (thread
        (lambda ()
          (sleep 2)
          (queue-callback
            (lambda ()
              (send message1 next-question))))))))

This version, instead of causing the main thread to block, some other
thread blocks and two seconds later that thread uses `queue-callback'
to run an event back on the main event-handling thread. Queue-callback installs it's argument onto the end of the event queue.

Matthias's solution is more complex to explain -- with his solution,
events may be handled (in a nested fashion) during the call to
sleep/yield; this means that the update event is handled, but so are
other events, such as button clicks, etc. This may have trigger race
conditions, depending on the rest of your program.

Of course, my solution also may need some tweaking depending on the
rest of your program. I suspect that you may need to disable part of
the interface while you are waiting the two seconds for the next
question to appear.

Hope that helps.

Robby