Re: calling SwingWorker cancel in WindowClosing event handler



yancheng.cheok@xxxxxxxxx wrote:
hello,

i realize that when after i call SwingWorker cancel and try to wait
until the task is really canceled (through SwingWorker.get), i will
get exception as stated in http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html

Note: If get is invoked on a SwingWorker object after its background
task has been cancelled, java.util.concurrent.CancellationException is
thrown.

Hence, i was wondering after the CancellationException caught, when
the time the next statement is executed, does the task is really
cancelled?

thanks

private void formWindowClosing(java.awt.event.WindowEvent evt)
{
// TODO add your handling code here:
try
{
marketTask.cancel(false);
marketTask.get();
}
catch(InterruptedException exp) {
}
catch(java.util.concurrent.ExecutionException exp)
{
}
catch(java.util.concurrent.CancellationException exp)
{
}

// So, does market Task really cancelled???
}


You will know that marketTask is canceled by checking the return value of cancel(). Calling get() will not block. It will just throw a CancellationException.

From the docs:

cancel

public final boolean cancel(boolean mayInterruptIfRunning)

Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • calling SwingWorker cancel in WindowClosing event handler
    ... i realize that when after i call SwingWorker cancel and try to wait ... get exception as stated in http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html ... catch(java.util.concurrent.ExecutionException exp) ...
    (comp.lang.java.gui)
  • calling SwingWorker cancel in WindowClosing event handler
    ... i realize that when after i call SwingWorker cancel and try to wait ... get exception as stated in http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html ... catch(java.util.concurrent.ExecutionException exp) ...
    (comp.lang.java.gui)
  • calling SwingWorker cancel in WindowClosing event handler
    ... i realize that when after i call SwingWorker cancel and try to wait ... get exception as stated in http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html ... catch(java.util.concurrent.ExecutionException exp) ...
    (comp.lang.java.gui)
  • Executors, Futures, and Cancelation
    ... So far, I have gotten a lot of use out of the API, and used it on a ... a method to cancel the task, ... for execution. ... call myCleanup() for me, or if it is still "in queue," and will never ...
    (comp.lang.java.programmer)
  • Re: DoCmd.Close command does not terminate form code execution
    ... executing a Close does not immediately terminate the procedure that is running. ... END OF FILE loop. ... true records the cancel event to an audit log, ... code execution stopped, but this is apprently not the case here. ...
    (microsoft.public.access.modulesdaovba)