| 1 |
'\" |
| 2 |
'\" Copyright (c) 1990-1994 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH after n 7.5 Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
after \- Execute a command after a time delay |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBafter \fIms\fR |
| 16 |
.sp |
| 17 |
\fBafter \fIms \fR?\fIscript script script ...\fR? |
| 18 |
.sp |
| 19 |
\fBafter cancel \fIid\fR |
| 20 |
.sp |
| 21 |
\fBafter cancel \fIscript script script ...\fR |
| 22 |
.sp |
| 23 |
\fBafter idle \fR?\fIscript script script ...\fR? |
| 24 |
.sp |
| 25 |
\fBafter info \fR?\fIid\fR? |
| 26 |
.BE |
| 27 |
.SH DESCRIPTION |
| 28 |
.PP |
| 29 |
This command is used to delay execution of the program or to execute |
| 30 |
a command in background sometime in the future. It has several forms, |
| 31 |
depending on the first argument to the command: |
| 32 |
.TP |
| 33 |
\fBafter \fIms\fR |
| 34 |
. |
| 35 |
\fIMs\fR must be an integer giving a time in milliseconds. |
| 36 |
A negative number is treated as 0. |
| 37 |
The command sleeps for \fIms\fR milliseconds and then returns. |
| 38 |
While the command is sleeping the application does not respond to |
| 39 |
events. |
| 40 |
.TP |
| 41 |
\fBafter \fIms \fR?\fIscript script script ...\fR? |
| 42 |
. |
| 43 |
In this form the command returns immediately, but it arranges |
| 44 |
for a Tcl command to be executed \fIms\fR milliseconds later as an |
| 45 |
event handler. |
| 46 |
The command will be executed exactly once, at the given time. |
| 47 |
The delayed command is formed by concatenating all the \fIscript\fR |
| 48 |
arguments in the same fashion as the \fBconcat\fR command. |
| 49 |
The command will be executed at global level (outside the context |
| 50 |
of any Tcl procedure). |
| 51 |
If an error occurs while executing the delayed command then |
| 52 |
the background error will be reported by the command |
| 53 |
registered with \fBinterp bgerror\fR. |
| 54 |
The \fBafter\fR command returns an identifier that can be used |
| 55 |
to cancel the delayed command using \fBafter cancel\fR. |
| 56 |
A \fIms\fR value of 0 (or negative) queues the event immediately with |
| 57 |
priority over other event types (if not installed withn an event proc, |
| 58 |
which will wait for next round of events). |
| 59 |
.TP |
| 60 |
\fBafter cancel \fIid\fR |
| 61 |
. |
| 62 |
Cancels the execution of a delayed command that |
| 63 |
was previously scheduled. |
| 64 |
\fIId\fR indicates which command should be canceled; it must have |
| 65 |
been the return value from a previous \fBafter\fR command. |
| 66 |
If the command given by \fIid\fR has already been executed then |
| 67 |
the \fBafter cancel\fR command has no effect. |
| 68 |
.TP |
| 69 |
\fBafter cancel \fIscript script ...\fR |
| 70 |
. |
| 71 |
This command also cancels the execution of a delayed command. |
| 72 |
The \fIscript\fR arguments are concatenated together with space |
| 73 |
separators (just as in the \fBconcat\fR command). |
| 74 |
If there is a pending command that matches the string, it is |
| 75 |
canceled and will never be executed; if no such command is |
| 76 |
currently pending then the \fBafter cancel\fR command has no effect. |
| 77 |
.TP |
| 78 |
\fBafter idle \fIscript \fR?\fIscript script ...\fR? |
| 79 |
. |
| 80 |
Concatenates the \fIscript\fR arguments together with space |
| 81 |
separators (just as in the \fBconcat\fR command), and arranges |
| 82 |
for the resulting script to be evaluated later as an idle callback. |
| 83 |
The script will be run exactly once, the next time the event |
| 84 |
loop is entered and there are no events to process. |
| 85 |
The command returns an identifier that can be used |
| 86 |
to cancel the delayed command using \fBafter cancel\fR. |
| 87 |
If an error occurs while executing the script then the |
| 88 |
background error will be reported by the command |
| 89 |
registered with \fBinterp bgerror\fR. |
| 90 |
.TP |
| 91 |
\fBafter info \fR?\fIid\fR? |
| 92 |
. |
| 93 |
This command returns information about existing event handlers. |
| 94 |
If no \fIid\fR argument is supplied, the command returns |
| 95 |
a list of the identifiers for all existing |
| 96 |
event handlers created by the \fBafter\fR command for this |
| 97 |
interpreter. |
| 98 |
If \fIid\fR is supplied, it specifies an existing handler; |
| 99 |
\fIid\fR must have been the return value from some previous call |
| 100 |
to \fBafter\fR and it must not have triggered yet or been canceled. |
| 101 |
In this case the command returns a list with two elements. |
| 102 |
The first element of the list is the script associated |
| 103 |
with \fIid\fR, and the second element is either |
| 104 |
\fBidle\fR or \fBtimer\fR to indicate what kind of event |
| 105 |
handler it is. |
| 106 |
.LP |
| 107 |
The \fBafter \fIms\fR and \fBafter idle\fR forms of the command |
| 108 |
assume that the application is event driven: the delayed commands |
| 109 |
will not be executed unless the application enters the event loop. |
| 110 |
In applications that are not normally event-driven, such as |
| 111 |
\fBtclsh\fR, the event loop can be entered with the \fBvwait\fR |
| 112 |
and \fBupdate\fR commands. |
| 113 |
.SH "EXAMPLES" |
| 114 |
This defines a command to make Tcl do nothing at all for \fIN\fR |
| 115 |
seconds: |
| 116 |
.PP |
| 117 |
.CS |
| 118 |
proc sleep {N} { |
| 119 |
\fBafter\fR [expr {int($N * 1000)}] |
| 120 |
} |
| 121 |
.CE |
| 122 |
.PP |
| 123 |
This arranges for the command \fIwake_up\fR to be run in eight hours |
| 124 |
(providing the event loop is active at that time): |
| 125 |
.PP |
| 126 |
.CS |
| 127 |
\fBafter\fR [expr {1000 * 60 * 60 * 8}] wake_up |
| 128 |
.CE |
| 129 |
.PP |
| 130 |
The following command can be used to do long-running calculations (as |
| 131 |
represented here by \fI::my_calc::one_step\fR, which is assumed to |
| 132 |
return a boolean indicating whether another step should be performed) |
| 133 |
in a step-by-step fashion, though the calculation itself needs to be |
| 134 |
arranged so it can work step-wise. This technique is extra careful to |
| 135 |
ensure that the event loop is not starved by the rescheduling of |
| 136 |
processing steps (arranging for the next step to be done using an |
| 137 |
already-triggered timer event only when the event queue has been |
| 138 |
drained) and is useful when you want to ensure that a Tk GUI remains |
| 139 |
responsive during a slow task. |
| 140 |
.PP |
| 141 |
.CS |
| 142 |
proc doOneStep {} { |
| 143 |
if {[::my_calc::one_step]} { |
| 144 |
\fBafter idle\fR [list \fBafter\fR 0 doOneStep] |
| 145 |
} |
| 146 |
} |
| 147 |
doOneStep |
| 148 |
.CE |
| 149 |
.SH "SEE ALSO" |
| 150 |
concat(n), interp(n), update(n), vwait(n) |
| 151 |
.SH KEYWORDS |
| 152 |
cancel, delay, idle callback, sleep, time |
| 153 |
'\" Local Variables: |
| 154 |
'\" mode: nroff |
| 155 |
'\" End: |