Received: by 10.180.100.225 with SMTP id fb1mr243300wib.3.1348819206077; Fri, 28 Sep 2012 01:00:06 -0700 (PDT) Path: q11ni83736163wiw.1!nntp.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!news.mixmin.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Uwe Klein Newsgroups: comp.lang.tcl Subject: Re: How to interrupt a task which is running in a thread? Date: Fri, 28 Sep 2012 09:46:53 +0200 Organization: A noiseless patient Spider Lines: 39 Message-ID: References: <9045bde7-7f2c-4b82-8fb0-5a58f7292e2c@googlegroups.com> <70923e8b-aa74-4b28-b655-255e6b06fbce@googlegroups.com> <5cd19719-a2df-4b8e-916c-f5e68a8f6cdb@googlegroups.com> Mime-Version: 1.0 Injection-Info: mx04.eternal-september.org; posting-host="3bc0ca029b2d3aac2bb35b2ba92ac093"; logging-data="12988"; mail-complaints-to="ab...@eternal-september.org"; posting-account="U2FsdGVkX1/bWad/SLiyTS2TpfqLj7fzsL0Oz2HIiR4=" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040114 In-Reply-To: <5cd19719-a2df-4b8e-916c-f5e68a8f6cdb@googlegroups.com> X-Accept-Language: en-us, en Cancel-Lock: sha1:o9VwJqEFd61WD9sLQlKI2ZMurEI= Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Andy wrote: > 在 2012年9月28日星期五UTC+8下午2时11分37秒,Gerald W. Lester写道: > >>The example you cited before, file copy, does not require threads -- in Tcl/Tk most things don't. Tcl is not Java -- make use of the event loop or co-routines not threads. Only use threads for things that either (a) block or (b) can/need to be done in parallel (and you have multiple cores/cpus). P.S. -- in the case of (b), it might be better to spawn off separate processes. On 9/27/12 9:34 PM, Andy wrote: > The task is still running even if I have released the thread. > > package require Thread > > set t1 [ thread::create { > proc test { } { > while {1} { > after 1000 > puts hello > } > } > thread::wait > }] > > thread::send -async $t1 [ list test ] > > puts [ thread::names ] > after 3000 > > thread::release $t1 > puts [ thread::names ] > vwait forever > -- +------------------------------------------------------------------------+ | Gerald W. Lester, President, KNG Consulting LLC | | Email: Gerald...@kng-consulting.net | +---------------------------------------------------- --------------------+ > > > Hi Gerald > > But if I don't use thread, my GUI will be stuck. Could you give me an example? simple example: #!/usr/bin/wish button .start -text Start -command {handlecmd X start} button .stop -text Stop -command {handlecmd X stop} grid .start .stop proc handlecmd {_id action} { upvar ::$_id id switch -- $action \ start { if {![catch {set id} cerr]} { puts stderr "Task runs already" return } set id [ exec ping localhost & ] } stop { if {[catch {set id} cerr]} { puts stderr "Task does not run" return } exec kill $id unset id } } uwe