Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to stop a running procedure?

21 views
Skip to first unread message

Chong Yie Chang

unread,
Nov 7, 2002, 3:41:53 AM11/7/02
to
Hi All:

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

Michael Barth

unread,
Nov 7, 2002, 4:50:39 AM11/7/02
to Chong Yie Chang
Chong Yie Chang wrote:
>
> Hi All:
>
> 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?


Have a look at http://mini.net/tcl/564

Michael

Michael Barth

unread,
Nov 8, 2002, 4:41:51 AM11/8/02
to Chong Yie Chang
"Chang, Chong Yie" wrote:
>
> Hi Mr. Barth:
>
> From your code in
>
> Interrupting loops <http://mini.net/tcl/564%21>
>
> Is you code only useful to
> cases, which run native programs, but
> not tcl procedures?
>
> I can not really understand your code totally.
> My tcl is not so good.
>
> Best regards,
>
> CCY
> # tcl code not tested:
> button .interrupt -command {close $FID}
> pack .interrupt
>
> set FID [open |userscript r]
> fileevent readable $FID writeToTextWidget

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

0 new messages