You're asking a great and subtle question!
“Instead of creating a separate thread, is it possible to treat callback from [place-channel-get] like it’s just another GUI element — if you know what I mean?”
Yes — you’re essentially asking: can we integrate place-channel messages into the GUI event loop directly, so they behave like button clicks or timer events — without spawning a background thread?
❓ Short Answer:
No — not directly.
Racket’s GUI event loop (racket/gui/base) does not natively understand place-channel as an event source like it does for button%, timer%, or even tcp-listener%.
BUT — ✅ you can make it feel that way using handle-evt + sync + queue-callback in a cleverly abstracted way — still requiring one background thread, but you can hide it behind a clean “callback-style” API that feels like native GUI integration.
🎯 Goal: Make Place Messages Feel Like GUI Events
You want to write something like:
(place-message-callback main-chan
(lambda (msg)
(send msg-editor insert (format "GUI got: ~a\n" msg))))
…and have it “just work”, without manually managing threads or queue-callback.
✅ Solution: One Background Thread + Event Handler Abstraction
We’ll create a helper function that spawns one reusable background thread per channel (or even globally), listens for messages, and invokes your callback — automatically wrapped in queue-callback for safety.
This hides the threading complexity and gives you a “GUI-native” feel.
💡 Implementation: start-place-channel-listener
#lang racket/gui
(require racket/place)
;; Global or per-channel listener manager
(define (start-place-channel-listener ch callback)
;; Runs in background thread
(thread
(lambda ()
(let loop ()
(define msg (place-channel-get ch))
(queue-callback
(lambda ()
(callback msg)))
(loop)))))
;; --- Example Usage ---
(define-values (main-chan place-chan)
(place-channel))
(define frame (new frame% [label "Place Listener Demo"] [width 300] [height 200]))
(define msg-text (new text%))
(new editor-canvas% [parent frame] [editor (send msg-text get-editor)])
(send frame show #t)
;; ✨ This feels like native GUI integration!
(start-place-channel-listener
main-chan
(lambda (msg)
(send (send msg-text get-editor) insert (string-append msg "\n"))))
;; Simulate place sending messages
(define p
(place ch
(let loop ([n 1])
(place-channel-put ch (format "Tick ~a" n))
(sleep 1)
(loop (add1 n)))))
(place-channel-put p place-chan)
🧠 Why This Feels Like “Just Another GUI Element”
Even though internally there’s a thread, from your perspective as a GUI programmer:
- You attach a callback to a channel.
- The callback runs in the GUI thread (thanks to
queue-callback).
- It’s non-blocking.
- You don’t manage threads or loops manually.
- It’s composable — you can have multiple listeners on different channels.
It’s analogous to:
(new button% [label "Click me"]
[callback (lambda (b e) ...)]) ; ← feels native, but internally uses OS events + event loop
Your start-place-channel-listener abstraction is now at the same level of convenience.
🔁 Bonus: Reusable Manager (Optional Advanced Pattern)
If you want to avoid spawning many threads (e.g., for many channels), you can create a single global event loop thread that syncs over multiple channels using choice-evt.
(define place-channel-listeners '())
(define (register-place-channel-listener ch callback)
(set! place-channel-listeners
(cons (cons ch callback) place-channel-listeners)))
;; Single background thread that syncs over all registered channels
(thread
(lambda ()
(let loop ()
(apply sync
(map (lambda (pair)
(handle-evt (car pair)
(lambda (msg)
(queue-callback
(lambda ()
((cdr pair) msg))))))
place-channel-listeners))
(loop))))
;; Now register listeners instead of starting threads
(register-place-channel-listener
main-chan
(lambda (msg)
(send (send msg-text get-editor) insert (string-append "Global: " msg "\n"))))
⚠️ Note: You’d need to enhance this with dynamic registration/removal and error handling for production use — but it shows the principle.
🚫 Why You Can’t Fully Avoid a Thread
Under the hood, place-channel-get is a blocking operation unless used with sync/timeout or handle-evt. But sync only works inside a context that’s already waiting — like a thread or the REPL.
The GUI event loop doesn’t expose its internal sync mechanism for arbitrary user-defined events (like place channels). So you must bridge the gap with at least one background thread.
✅ Final Answer
You cannot treat place-channel-get as a native GUI event source without any background thread — but you can abstract it away so that from your code’s perspective, it feels exactly like attaching a callback to a button or timer.
Use start-place-channel-listener (or a global manager) to encapsulate the thread and queue-callback, giving you clean, idiomatic, GUI-thread-safe callbacks.
This is how real-world Racket GUI apps handle async I/O, network events, or place communication — hiding concurrency behind simple callback interfaces.
Let me know if you want to extend this to support unregistering listeners, error handling, or bidirectional communication — happy to help!