1after(n) Tcl Built-In Commands after(n)
2
3
4
5______________________________________________________________________________
6
8 after - Execute a command after a time delay
9
11 after ms
12
13 after ms ?script script script ...?
14
15 after cancel id
16
17 after cancel script script script ...
18
19 after idle ?script script script ...?
20
21 after info ?id?
22_________________________________________________________________
23
24
26 This command is used to delay execution of the program or to execute a
27 command in background sometime in the future. It has several forms,
28 depending on the first argument to the command:
29
30 after ms
31 Ms must be an integer giving a time in milliseconds. The com‐
32 mand sleeps for ms milliseconds and then returns. While the
33 command is sleeping the application does not respond to events.
34
35 after ms ?script script script ...?
36 In this form the command returns immediately, but it arranges
37 for a Tcl command to be executed ms milliseconds later as an
38 event handler. The command will be executed exactly once, at
39 the given time. The delayed command is formed by concatenating
40 all the script arguments in the same fashion as the concat com‐
41 mand. The command will be executed at global level (outside the
42 context of any Tcl procedure). If an error occurs while execut‐
43 ing the delayed command then the background error will be
44 reported by the command registered with interp bgerror. The
45 after command returns an identifier that can be used to cancel
46 the delayed command using after cancel.
47
48 after cancel id
49 Cancels the execution of a delayed command that was previously
50 scheduled. Id indicates which command should be canceled; it
51 must have been the return value from a previous after command.
52 If the command given by id has already been executed then the
53 after cancel command has no effect.
54
55 after cancel script script ...
56 This command also cancels the execution of a delayed command.
57 The script arguments are concatenated together with space sepa‐
58 rators (just as in the concat command). If there is a pending
59 command that matches the string, it is cancelled and will never
60 be executed; if no such command is currently pending then the
61 after cancel command has no effect.
62
63 after idle script ?script script ...?
64 Concatenates the script arguments together with space separators
65 (just as in the concat command), and arranges for the resulting
66 script to be evaluated later as an idle callback. The script
67 will be run exactly once, the next time the event loop is
68 entered and there are no events to process. The command returns
69 an identifier that can be used to cancel the delayed command
70 using after cancel. If an error occurs while executing the
71 script then the background error will be reported by the command
72 registered with interp bgerror.
73
74 after info ?id?
75 This command returns information about existing event handlers.
76 If no id argument is supplied, the command returns a list of the
77 identifiers for all existing event handlers created by the after
78 command for this interpreter. If id is supplied, it specifies
79 an existing handler; id must have been the return value from
80 some previous call to after and it must not have triggered yet
81 or been cancelled. In this case the command returns a list with
82 two elements. The first element of the list is the script asso‐
83 ciated with id, and the second element is either idle or timer
84 to indicate what kind of event handler it is.
85
86 The after ms and after idle forms of the command assume that the appli‐
87 cation is event driven: the delayed commands will not be executed
88 unless the application enters the event loop. In applications that are
89 not normally event-driven, such as tclsh, the event loop can be entered
90 with the vwait and update commands.
91
93 This defines a command to make Tcl do nothing at all for N seconds:
94 proc sleep {N} {
95 after [expr {int($N * 1000)}]
96 }
97
98 This arranges for the command wake_up to be run in eight hours (provid‐
99 ing the event loop is active at that time):
100 after [expr {1000 * 60 * 60 * 8}] wake_up
101
102 The following command can be used to do long-running calculations (as
103 represented here by ::my_calc::one_step, which is assumed to return a
104 boolean indicating whether another step should be performed) in a step-
105 by-step fashion, though the calculation itself needs to be arranged so
106 it can work step-wise. This technique is extra careful to ensure that
107 the event loop is not starved by the rescheduling of processing steps
108 (arranging for the next step to be done using an already-triggered
109 timer event only when the event queue has been drained) and is useful
110 when you want to ensure that a Tk GUI remains responsive during a slow
111 task.
112 proc doOneStep {} {
113 if {[::my_calc::one_step]} {
114 after idle [list after 0 doOneStep]
115 }
116 }
117 doOneStep
118
119
121 concat(n), interp(n), update(n), vwait(n)
122
123
125 cancel, delay, idle callback, sleep, time
126
127
128
129Tcl 7.5 after(n)