Previous section   Next section

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 16.  Event-Driven Programming

The after Command

The after command sets up commands to happen in the future. In its simplest form, it pauses the application for a specified time, in milliseconds. The example below waits for half a second:

after 500

During this time, the application does not process events. You can use the vwait command as shown on page 220 to keep the Tcl event loop active during the waiting period. The after command can register a Tcl command to occur after a period of time, in milliseconds:

after milliseconds cmd arg arg...

The after command treats its arguments like eval; if you give it extra arguments, it concatenates them to form a single command. If your argument structure is important, use list to build the command. The following example always works, no matter what the value of myvariable is:

after 500 [list puts $myvariable]

The return value of after is an identifier for the registered command. You can cancel this command with the after cancel operation. You specify either the identifier returned from after, or the command string. In the latter case, the event that matches the command string exactly is canceled.

Table 16-1 summarizes the after command:

Table 16-1. The after command.
after millisecondsPauses for milliseconds.
after ms arg ?arg...?Concatenates the args into a command and executes it after ms milliseconds. Immediately returns an ID.
after cancel idCancels the command registered under id.
after cancel commandCancels the registered command.
after idle commandRuns command at the next idle moment.
after info ?id?Returns a list of IDs for outstanding after events, or the command associated with id.

      Previous section   Next section