Re: [tcljava-user] How to kill a jacl thread
Brought to you by:
mdejong
From: Bruce J. <nm...@ma...> - 2009-01-29 18:55:11
|
One approach I use in applications using Swank is whenever I do something that will take a long time, but might need to be cancelled is to use the following procs (which can be cleaned up a bit, but basically work). Before starting the "job" I call NvCounterMake, then within the long running job periodically call NvCounterCheck. Every time it is called it will update a counter that is displayed (hence the name for the procs) and check the status of a global variable named "int_flag". Pushing the Quit button will set the int_flag. In the implementation below the NvCounterCheck proc will throw an error if int_flag is set. There are more graceful ways to exit, but this is probably safer than just blowing away a Thread. Obviously this won't work if your not working in Swank, but the general principle holds that Threads shouldn't be killed, some flag should be set so they know to gracefully exit. Bruce proc NvCounterMake {title} { global Nv_Priv set pwin .vecGraph.processor.border if {[winfo exists $pwin]} { set cwin $pwin.nvCounter set top 0 } else { set top 1 set cwin .nvCounter } global int_flag NvCounter set int_flag 0 if {[winfo exists $cwin] } {destroy $cwin} if {$top} { toplevel $cwin wm withdraw $cwin wm title $cwin $title set x [expr [winfo screenwidth .]/2] set y [expr [winfo screenheight .]/4] } else { frame $cwin pack $cwin -side left } label $cwin.lab -textvariable NvCounter1 -width 10 pack $cwin.lab -side left button $cwin.quit -text Quit -command "set int_flag 1;destroy $cwin" pack $cwin.quit -side right if {$top} { wm deiconify $cwin wm geometry $cwin 200x20+$x+$y wm alwaysontop $cwin 1 raise $cwin } set Nv_Priv(counterWin) $cwin set NvCounter 1 update } ###################################################################### proc NvCounterCheck {} { global NvCounter NvCounter1 int_flag incr NvCounter set NvCounter1 $NvCounter if {($NvCounter % 200) == 0} { update } if {$int_flag} { NvCounterDestroy error "Interrupted" #return -code break } } proc NvCounterDestroy {} { global Nv_Priv int_flag catch "destroy $Nv_Priv(counterWin)" catch {after cancel $Nv_Priv(afterCounter)} } On Jan 29, 2009, at 11:29 AM, Jared Hodge wrote: > Is there a good way to kill a jacl thread quickly from another > thread? I’m launching a large script and that has many calls to > custom java code (some of which take a while themselves), and > sometimes I want to just kill the whole thread off and start another > one. The problem is that the doOneEvent call seems to not exit > until the script is complete and so I can’t find a good way of > getting the run thread to exit. It seems that all other ways of > exiting a thread have been deprecated. Ideally I’d like to stop the > thread dead in its tracks in the middle of a java call, but that’s > probably beyond the scope of this mailing list. A reasonable > alternative is to stop between evaluations in the jacl interpreter, > but I need some sort of low-level hook inside the doOneEvent call. > Does this already exist? > > Thanks > -Jared > ------------------------------------------------------------------------------ > This SF.net email is sponsored by: > SourcForge Community > SourceForge wants to tell your story. > http://p.sf.net/sfu/sf-spreadtheword_______________________________________________ > tcljava-user mailing list > tcl...@li... > https://lists.sourceforge.net/lists/listinfo/tcljava-user |