I am having some problems cancelling a swing worker. I am unsure after reading the documentation how to approach solving this
sw = new SwingWorker<Object, Void>()
{
Object o;
protected Object doInBackground() throws Exception
{
//do some long running task via a method call
}
}
Then in my cancel button handler i just want to call sw.cancel(true);
This doesnt seem to be working though. According to the documentation the swing worker needs to no how to cancel iteslf. But, if the method to run the swing worker is only being calld once then how do i account for this with thread.sleep(which is what i’ve read is one way of fixing)
Thanks
Swing will actually monitor your work with a Future. When you try to cancel the worker Swing will invoke
future.cancel(true)(the true value will attempt tointerrupt()the executing thread).What this means is your work needs to respond to interruption. If the long running computations don’t respond to interruption you will have to put in some checkpoints and respond accordingly
Swing will simply notify the executing thread that it should cancel. It is your responsibility to actually enforce cancellation.