Package: glib

Callback glib:source-func

Syntax

lambda () => result

Arguments

result -- false if the source should be removed, the g:+source-continue+ and g:+source-remove+ constants are memorable names for the return value

Details

Specifies the type of callback function passed to the g:timeout-add, and g:idle-add functions.

Examples

This example shows a timeout callback function, which runs 10 times and then quits the main loop.
(let ((counter 0) (max 10))
  (defun timeout-callback (loop)
    (incf counter)
    (if (>= counter max)
        (progn
          ;; Reset the counter
          (setf counter 0)
          ;; Stop the main loop from running
          (g:main-loop-quit loop)
          ;; Stop the source
          g:+source-remove+)
        ;; Continue the source
        g:+source-continue+)))

(defun example-timeout-source () (let* ((context (g:main-context-new)) (mainloop (g:main-loop-new context nil)) ;; Create a new timeout source (source (g:timeout-source-new 10))) ;; Attach source to context (g:source-attach source context) ;; Set the callback for source (g:source-set-callback source (lambda () (timeout-callback mainloop))) ;; Start the main loop (g:main-loop-run mainloop) (g:main-loop-unref mainloop)))
 

See also

2024-11-6