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