I am trying to create two buttons called Run and Stop.
I hope that the stop button will envoke a command,
which can stop running procedure. I failed. Can any body give me
a idea?
Best regards,
Chang, Chong Yie
set afterID 0;
proc TestBta {}\
{
global afterID;
for {set i 0} {$i < 30} {incr i}\
{
puts stdout "$i";
if {$::stop == 1}\
{
after cancel $afterID;
return 0;
}
update idletasks;
after 200;
}
return 0;
}
proc NeverStop {}\
{
global afterID;
set afterID [after 0 TestBta];
return 0;
}
catch {destroy .bta .btb};
set bta [button .bta -text "Test" -command {NeverStop}]
set btb [button .btb -text "Cancel" -command [list puts stdout "Stop"]];
pack .bta
pack .btb;
set ::stop "0";
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Have a look at http://mini.net/tcl/564
Michael
The reason for your problems is the the principle Tcl executes the code.
Tcl has an event handler which executes the code if an event occurs. For
example with the [after] command you add code to the event handler.
Other commands which add code to the event handler are for example
[bind], [tkwait], [vwait], the -command option of the [button] command,
...
The important thing for you is that after the event has fired the
command it cannot be interrupted from outside . But there are solutions
to overcome these problems (->Wiki).
One solution is the code which I mentioned in my last mail. Do the
following steps:
1. Write your code which you want to interrupt in an extra file. Let's
say the file name is userscript.
2. The file must be an executable Tcl-Script.
3. Write the code above in your main program.
4. Write a procedure writeToTextWidget. In there you can read ([read]
command) all stuff of stdout from your userscript ([puts] command) and
handle these answers.
In principle you can do that with foreign programms too. May be closing
the open pipe line ([close]) is not enough. Killing the process will
help (UNIX: exec kill [pid $FID], or package TclX).
I hope that helps.
Michael