Results 1 to 15 of 15
Thread: How to stop SwingWorker?
- 07-19-2010, 11:40 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 7
- Rep Power
- 0
How to stop SwingWorker?
Good day everybody!
I need a piece of advice on how to stop SwingWorker because using cancel(true) method doesn't work out.
Here's the outline: everyting runs fine. When I push the button on the UI (i.e. call the cancel(true) method) the program keeps running as if nothing has happened and runs safely till the very last calculation.
Furthermore, I've inserted a beeper into the done() method so that it beeps every time done() is called. And, surprisingly for me, it beeps every time I push the button (i.e. call the cancel() method).Afterwards the program keeps running and then it beeps at the end. I don't get it, why it beeps after I push the button if calling a done() method should imply the end of SwingWorker?
And what's more important to me, how can I interrupt the SwingWorker in this case?
I'd be grateful for any help.
- 07-19-2010, 11:53 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you show what you've done, how did you do that?
- 07-19-2010, 12:37 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 7
- Rep Power
- 0
Sure.
Here's the listener added to the UI button:
Here's the done() method of MySwingWorker:Java Code:class StartStopListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { MySwingWorker sct = new MySwingWorker(); if (startBtn.getText().equals("START")) { sct.execute(); } else { // button's name "STOP" -- button's name is switched START-STOP sct.cancel(true); } } } }
Here's the doInBackground() method:Java Code:public void done() { Toolkit.getDefaultToolkit().beep(); }
Java Code:public Void doInBackground() { float prococ; float output; for (int r=0; r<allRuns; r++) { setAnew(r+1); // sets all variable values anew for(int t=0; t<allTime; t++) { for(int h=0; h<popSize; h++) { setAgentAlive(ppl[h]); // main calculations } output=prococ/popSize/t; // some indicator mFrame.graphPanel.updateGraphMem(output); // draws a live graph on the UI String daytime = Integer.toString((t+1)%24)+":00"; int progr = (int)(100*(runNum-1)/(float)allRuns+100*(t+1)/(float)allTime/allRuns); publish(new Sentee(daytime, "Society mood: "+Float.toString(output), output, progr)); // displays some info for a user } } return null; }
- 07-19-2010, 03:02 PM #4
Have you tried debugging your code by adding println()s to show where and what the code is doing?
Can the loop in the SwingWorker test to see if its thread has been interrupted?
- 07-19-2010, 03:22 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 7
- Rep Power
- 0
Norm
Yes, I did some debugging but being not sure what exactly I was looking for I haven't noticed anything suspicious. Besides, the actual code is more lengthy (because I've omitted here some methods from doInBackground()) so I couldn't just blindly check every variable or line of code. Can you give me a hint what I should be looking for?
I've tried adding a checking line "if (isCancelled()) return null;" right at the beginning of each for-loop but that didn't help much.
- 07-19-2010, 03:27 PM #6
Crosspost: Swing - How to stop SwingWorker running?
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-19-2010, 03:33 PM #7
For example you said there were beeps. Did you have a println() in the done() method to see if the beep and the println() matched?not sure what exactly I was looking for I haven't noticed anything suspicious.
What other Thread flags did you test in the loop?
How much? Did it ever happen or only once every 20 tests or ???but that didn't help much.
How did you get a reference to the SwingWorker object to call the isCancelled() method?
Try making a small executable program that demos the problem we can test it.
- 07-19-2010, 05:38 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 7
- Rep Power
- 0
I have now after you've suggested. Just a simple phrase like "Done is called" to check, right? And yes they matched. Every time I heard a beep I got this message.
Not sure what you mean by thread flags. Sorry.
This bug (or my mistake) happens every time I run the program and try to stop it the way I've described.
Just like that:
Java Code:... setAnew(r+1); // sets all variable values anew for(int t=0; t<allTime; t++) { if (isCancelled()) return null; for(int h=0; h<popSize; h++) { if (isCancelled()) return null; setAgentAlive(ppl[h]); // main calculations } ...
Because there are a lot of omitted enclosed methods in this program I'm not sure I can make a small demo. But I'll see what I can do.
Thank you for your willingness to help, Norm.Last edited by JStarter; 07-19-2010 at 05:51 PM.
- 07-19-2010, 05:51 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
This is the code in the aciton listener.Java Code:MySwingWorker sct = new MySwingWorker(); if (startBtn.getText().equals("START")) { sct.execute(); } else { // button's name "STOP" -- button's name is switched START-STOP sct.cancel(true); }
Assuming the button is STOP, what swing worker are you cancelling?
Just walk through it...
- 07-19-2010, 06:03 PM #10
An example of a usage of debugging techniques using println(). One println() in a constructor would show what is happening.
- 07-20-2010, 08:50 AM #11
Member
- Join Date
- Jul 2010
- Posts
- 7
- Rep Power
- 0
Tolls
What do you mean, 'which SwingWorker'? If you mean the same as Maxideon (Swing - How to stop SwingWorker running?) then yes, it was an embarassingly bad mistake of mine. :) But as I said on that forum: it seems, I couldn't correct it.
Sorry for crossposting again (I was a bit desperate when started a new thread on the second forum).
Norm, I'm afaraid I didn't get what exactly you're suggesting. Could you provide a link so I can read more on that technique?
- 07-20-2010, 09:09 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Read the SwingWorker tutorial.
Imagine the SwingWorker is on your for loop...where in that loop can it exit?
- 07-20-2010, 10:15 AM #13
Member
- Join Date
- Jul 2010
- Posts
- 7
- Rep Power
- 0
Tolls
Thank you very much. Another thing I did wrong was deleting lines "if (isCancelled()) return null;". Now that I put them back the program stops fine.
But I still don't understand why I should use isCancelled() method at all if cancel(true) method is supposed to interrupt the SwingWorker no matter what it is doing. What topic should I study to clarify this?
Anyway, the problem is solved and I appreciate very much every effort to help me. Thank you all.
- 07-20-2010, 10:25 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
A SwingWorker starts another Thread so that the code in the doInBackGround() method doesn't run in the EDT thread. All you can do is interrupt() a Thread but it doesn't stop it, i.e. if the Thread was sleeping it is woken up; and its 'interrupted' flag is set (even if it wan't sleeping). The cancel() method does just that plus it sets a private boolean variable which your code can test (running in that other thread). The isCanceled() method is used for that. If your code ignores that all it simply keeps on running with that silly little 'interrupted' flag set and the boolean variable 'cancel' set to true. That's why your code in the doInBackGround() method should periodically test the condition and stop if the condition is met.
kind regards,
Jos
- 07-20-2010, 04:36 PM #15
Member
- Join Date
- Jul 2010
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Using SwingWorker
By viking90 in forum New To JavaReplies: 1Last Post: 04-24-2010, 09:17 AM -
SwingWorker question
By cotarelo in forum Threads and SynchronizationReplies: 16Last Post: 03-23-2010, 11:29 AM -
SwingWorker Problem
By Berkan in forum Threads and SynchronizationReplies: 10Last Post: 03-11-2010, 03:28 AM -
SwingWorker Opinions
By frejon26 in forum AWT / SwingReplies: 3Last Post: 04-13-2009, 08:41 PM -
swingworker
By musiigedeo in forum AWT / SwingReplies: 1Last Post: 07-26-2007, 12:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks