java - Cancel anonymous SwingWorker -
i have run weird dependency when trying cancel anonymous swingworker. current code:
private static void init() { if (connected) { return; } //final swingworker<void, void> initworker; final timer noconnectiontimer = new timer(5000, new actionlistener() { @override public void actionperformed(actionevent e) { //initworker.cancel(true); waitobject.settimedout(true); connected = false; waitobject.process(); } }); new swingworker<void, void>() { @override public void doinbackground() { noconnectiontimer.setrepeats(false); noconnectiontimer.start(); cachedinstance = new network(config.host_name, config.host_port); if (connected) { noconnectiontimer.stop(); new thread(cachedinstance).start(); waitobject.settimedout(false); waitobject.process(); } return null; } }.execute(); }
first assumption want verified:
i can kill new network(config.host_name, config.host_port)
code killing (cancelling) swingworker.
so assuming assumption correct, want cancel anonymous swingworker, when try give name initworker
, needs final such timer can reference it. swingworker needs reference timer aswell, timer needs final.
so think managed create dependancy between 2 final variables need eachother, how can fix this?
regards.
you don't have add actionlistener
upon creation. pass null
timer
's constructor , add actionlistener
afterwards, using addactionlistener
.
Comments
Post a Comment