On 27/04/2012 19:16, jmc wrote:
> When trying to understand "tkwait variable", is it possible to say :
It's exactly the same as [vwait varname].
> When the command "tkwait variable varname" is executed,
>
> 1) varname is (re)created and set to nothing (empty string ?)
It's not created, though its variable record *is* so that a trace can be
set on it. What is set is a write trace (maybe an unset trace too; I'd
have to check the code to be sure).
> 2) a repeating polling begins in bakground to see if varname is set
> (event loop)
The event loop is actually a little more efficient than that, but yes.
It's effectively like this:
set ::varWasSet 0
trace add variable varname write {set ::varWasSet 1; # ignore rest}
while {!$::varWasSet} {
update
}
Except that it's done in C so it doesn't need global variables and can
use the options to the event loop code that make it wait for events.
> 3) until varname is set, the interpreter is blocked in executing
> further commands in the script following "tkwait variable varname"
Yes. It _waits_.
> 4) when varname is set, the associated event loop is destroyed
> (polling varname stops) but varname is not destroyed, and the
> interpreter continues execution following commands in the script.
Yes (except that the event loop isn't really destroyed; it's a loop that
calls into the event loop; let's be precisely correct). We most
certainly don't bother to unset the variable; you could always add that
yourself in your script if desired.
The other types of [tkwait] (visibility and window) are just the same,
except they have different triggers (<Visibility> and <Destroy> events,
respectively). They're not used nearly so much now, especially true for
[tkwait visibility] as that's not portable (not all platforms produce
the relevant event analog).
A consequence of this is that running [tkwait] (or [vwait]) in an event
can easily end up creating a concealed nesting of event loops, which
will clog up the stack and cause much grief. This is discussed on the
Wiki at
http://wiki.tcl.tk/1255 (it mostly talks about [update], but as
we know from above, the waits are really wrappers around the same
functionality, and have the same problems).
Donal.