Results 1 to 15 of 15
  1. #1
    JStarter is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Unhappy 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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    JStarter is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    Sure.

    Here's the listener added to the UI button:
    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 done() method of MySwingWorker:
    Java Code:
    public void done() {
       Toolkit.getDefaultToolkit().beep();
    }
    Here's the doInBackground() method:
    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;
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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?

  5. #5
    JStarter is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    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.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    not sure what exactly I was looking for I haven't noticed anything suspicious.
    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?

    What other Thread flags did you test in the loop?
    but that didn't help much.
    How much? Did it ever happen or only once every 20 tests or ???
    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.

  8. #8
    JStarter is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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?
    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.

    Quote Originally Posted by Norm View Post
    What other Thread flags did you test in the loop?
    Not sure what you mean by thread flags. Sorry.

    Quote Originally Posted by Norm View Post
    How much? Did it ever happen or only once every 20 tests or ???
    This bug (or my mistake) happens every time I run the program and try to stop it the way I've described.

    Quote Originally Posted by Norm View Post
    How did you get a reference to the SwingWorker object to call the isCancelled() method?
    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
       }
    ...

    Quote Originally Posted by Norm View Post
    Try making a small executable program that demos the problem we can test it.
    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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    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);
             }
    This is the code in the aciton listener.
    Assuming the button is STOP, what swing worker are you cancelling?

    Just walk through it...

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    An example of a usage of debugging techniques using println(). One println() in a constructor would show what is happening.

  11. #11
    JStarter is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    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?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Read the SwingWorker tutorial.

    Imagine the SwingWorker is on your for loop...where in that loop can it exit?

  13. #13
    JStarter is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    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.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JStarter View Post
    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?
    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

  15. #15
    JStarter is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Using SwingWorker
    By viking90 in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 09:17 AM
  2. SwingWorker question
    By cotarelo in forum Threads and Synchronization
    Replies: 16
    Last Post: 03-23-2010, 11:29 AM
  3. SwingWorker Problem
    By Berkan in forum Threads and Synchronization
    Replies: 10
    Last Post: 03-11-2010, 03:28 AM
  4. SwingWorker Opinions
    By frejon26 in forum AWT / Swing
    Replies: 3
    Last Post: 04-13-2009, 08:41 PM
  5. swingworker
    By musiigedeo in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 12:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •