我如何使用“after ms script"?在TCL? [英] How do I use "after ms script" in TCL?
问题描述
我正在尝试弄清楚如何在 TCL 8.6.0 版中使用after".这是我尝试过的:
I'm trying to figure out how to use "after" in TCL version 8.6.0. Here's what I attempted:
proc async {} {
set state waiting
puts "pre-timeout $state"
set id [after 1000 {set state timeout}]
puts "wait-timeout $state"
after 2000
puts "post-timeout $state"
}
async
输出:
pre-timeout waiting
wait-timeout waiting
post-timeout waiting
预期输出:
pre-timeout waiting
wait-timeout waiting
post-timeout timeout
推荐答案
after 命令令人困惑的部分是它有两种截然不同的形式.如果你说
The confusing part about the after command is that it has two very distinct forms. If you say
after 2000
然后执行停止 2000 毫秒,然后在下一个命令处恢复.
then execution stops for 2000 ms and then resumes at the next command.
如果你说
after 1000 {set ::state timeout}
然后set命令在1000毫秒后执行但必须进入事件循环.进入事件循环的方式是
then the set command is executed after 1000 ms but you must enter the event loop. The way to enter the event loop is
vwait ::state
最后要记住的是,after 脚本在全局级别执行,在任何过程之外.试试下面的代码:
The final thing to remember is that the after script executes at the global level, outside of any procedure. Try the following code:
proc async {} {
set ::state waiting
puts "pre-timeout $::state"
set id [after 1000 {set ::state timeout}]
puts "wait-timeout $::state"
vwait ::state
puts "post-timeout $::state"
}
async
这篇关于我如何使用“after ms script"?在TCL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!