Reputation: 223
In TCL do we have any mechanism that will keep polling for the variable change and perform certain action after that.
I have read about vwait
but it is pausing the script. I want the script to be running and in between if the variable value changes, perform certain action.
Kind of asynchronous mode of vwait.
Upvotes: 2
Views: 1140
Reputation: 2256
You could also use a recursive procedure to keep polling for the current value of a variable at a particular interval and bow out of the recursion once a specific condition for the variable is met.
For Example :
set x 1
proc CheckVariableValue {
global x
if { $x >= 5 } {
puts "end"
return 1;
}
else{
incr x
puts $x
after 1000 CheckVariableValue
}
}
CheckVariableValue
Upvotes: 0
Reputation: 137567
You can attach a trace to a variable so that you can do something immediately whenever the variable is changed (or, depending on flags, read from or deleted). Try out this example:
set abc 123
proc exampleCallback args {
global abc
puts "The variable abc is now $abc"
}
trace add variable abc write exampleCallback
incr abc
incr abc
incr abc
It's possible to trace local variables, but not recommended. Also, internally, the vwait
command sets a trace that just trips a flag when the variable is written to; that flag signals the wait to end when the event loop is returned to. It just happens that that trace is set using Tcl's C API, not its script-level API…
Upvotes: 5