Ben Greenman
Ben Greenman

Reputation: 2002

racket/gui: sleep without freezing gui

I'm writing a unit test and I want to:

  1. open a frame
  2. wait a few seconds
  3. close the frame

Here's the code I tried:

#lang racket/base
(require plot racket/class)

(define f
  (plot3d-frame (surface3d (λ (x y) (* (cos x) (sin y))) -3.0 3.0 -3.0 3.0)))
(send f show #true)
(sleep 10)
(send f show #false)

Running this opens a blank frame, waits 10 seconds, and closes the frame. The plot never appears. I guess this is because sleep puts the whole thread, including the eventspace, to sleep.

Is there a way to make my code sleep without making the GUI to sleep?

Upvotes: 1

Views: 518

Answers (1)

Ben Greenman
Ben Greenman

Reputation: 2002

Yes, use sleep/yield

#lang racket/base
(require plot racket/class racket/gui/base)

(define f
  (plot3d-frame (surface3d (λ (x y) (* (cos x) (sin y))) -3.0 3.0 -3.0 3.0)))
(send f show #true)
(sleep/yield 10)
(send f show #false)
;; "It works every time!"

Upvotes: 1

Related Questions