user1492793
user1492793

Reputation: 49

after cancel not working in tcl

I have written the following code to test after cancel . This code is supposed to print "Now updating" only once but it is printing it 10 times so can anyone tell me why after cancel not working

proc update_now {} {
    puts "Now updating"
}
proc print_now {} {
    after cancel [update_now]
    after idle [update_now]
}
for {set count 0} {$count < 5} {incr count} {
    print_now
}

Upvotes: 0

Views: 581

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137717

You are very confused. Firstly, you are using both after idle and after cancel on the result of calling update_now (an empty string as it happens, which is a no-op script) instead of on a script to call update_now. Tcl's very strict about reference vs use. Instead, you want something more like:

proc print_now {} {
    after cancel update_now
    after idle update_now
    # You could use {update_now} or [list update_now] too; no real difference here
}

Secondly, you should really focus on canceling only by token rather than according to what you search for. To do that, you actually do this:

proc print_now {} {
    global print_now_token
    after cancel $print_now_token
    set print_now_token [after idle update_now]
}
# Initialise the variable
set print_now_token {}

Upvotes: 2

Related Questions