Tcl after Command
last modified April 3, 2025
The Tcl after
command schedules script execution after a time
delay. It's essential for creating timers and delayed actions in Tcl. The
command works with both milliseconds and script callbacks.
Basic Definition
The after
command delays script execution or returns timer IDs.
It can schedule one-time or repeating events. The command integrates with
Tcl's event loop.
Syntax: after ms ?script?
. With one argument, it pauses execution.
With two arguments, it schedules script execution after ms milliseconds.
Simple Delay
This example shows the basic delay functionality of the after
command.
puts "Start" after 2000 puts "End"
This script prints "Start", waits 2000 milliseconds (2 seconds), then prints
"End". The after
command with one argument pauses execution.
Scheduled Script Execution
This demonstrates scheduling a script to run after a delay while the main script continues.
puts "Main script starts" after 1000 {puts "Delayed message"} puts "Main script continues" vwait forever
The main script starts, schedules a message to appear after 1 second, then
continues. The vwait
keeps the event loop running to process
the delayed script.
Canceling a Scheduled Event
The after cancel
command cancels a previously scheduled event
using its ID.
set id [after 3000 {puts "This won't appear"}] after 1000 {after cancel $id; puts "Event canceled"} vwait forever
This schedules a message for 3 seconds but cancels it after 1 second. The
after cancel
command uses the ID returned by the first after
.
Repeating Timer
This example creates a repeating timer by having the callback reschedule itself.
proc repeat {} { puts "Tick [clock format [clock seconds] -format %T]" after 1000 repeat } repeat vwait forever
The repeat
procedure prints the current time every second. Each
invocation schedules itself to run again after 1000 milliseconds.
Multiple Timers
This shows how to manage multiple independent timers with different intervals.
proc timer1 {} { puts "Timer1: [clock seconds]" after 1500 timer1 } proc timer2 {} { puts "Timer2: [clock seconds]" after 2500 timer2 } timer1 timer2 vwait forever
Two independent timers run with different intervals (1.5s and 2.5s). Each
timer maintains its own scheduling by calling after
in its
callback.
After with Arguments
This demonstrates passing arguments to a callback procedure using after
.
proc callback {msg count} { puts "$msg $count" incr count after 1000 [list callback $msg $count] } callback "Count:" 1 vwait forever
The callback
procedure receives and increments a counter. The
list
command ensures proper argument passing when scheduling
the next execution.
Best Practices
- Units: Remember time is in milliseconds (1000ms = 1s).
- Event Loop: Use
vwait
to keep the event loop running. - Memory: Cancel timers when no longer needed.
- Arguments: Use
list
for callback arguments. - Nesting: Avoid deep nesting of timed callbacks.
This tutorial covered the Tcl after
command with practical
examples showing timer creation, delayed execution, and event scheduling.
Author
List all Tcl Tutorials.