Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2011
    Location
    Florida
    Posts
    60
    Rep Power
    0

    Default SwingWorker background task pause

    I have successfully implemented a swingWorker to process a very long task in the background in my GUI. I have a button that allows the user to abort the process while it is running and that works fine. However, I would also like to add a button for the user to be able to temporarily suspend the background task and then continue. When I do that, the task gets suspended forever in the "while (pauseProcessing || abortProcessing)" loop and cannot be restarted again. Here is how I implemented it:

    Java Code:
    // The SwingWorker class
    private class ProcessFile extends SwingWorker<Boolean, Void> {
        @Override
        public Boolean doInBackground() {
           return processFileList();
        }
    
        @Override
        protected void done() {
            try {
                if (this.get())
                    statusBar.setText("done");
                else statuBar.setText("error");
            } catch (Exception e) {
                statusBar.setText("error while processing");
            }
        }
    }
    
    // The background task
    private boolean processFileList() {
            for (int i = 0; i < numofFiles; ++i)
            {
                while (pauseProcessing || abortProcessing)
                {
                    if (abortProcessing)
                    {
                        errCode = errorCodes.errProcessAborted;
                        return false;
                    }
                }
                lots more processing here...
            }
            return true;
    }
    
    // Button listeners
    private void abortButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        if (dirList != null)
            abortProcessing = true;
    }                                           
    
    private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {
        pauseProcessing = !pauseProcessing;
    }
    If this method for temporarily suspending the background task is incorrect, what is the correct method?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    <Sorry -- never mind previous post: bad advice!>

    Better advice: create an SSCCE.
    Last edited by Fubarable; 05-07-2011 at 05:30 PM.

  3. #3
    Join Date
    Feb 2011
    Location
    Florida
    Posts
    60
    Rep Power
    0

    Default

    I'm not sure I understand. Can you show me?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Does your pause while loop have a short Thread.sleep in it? It should else it will tie up your app. Found this out in my SSCCE:

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class SimplePauseProcessing extends JPanel {
       private static final int INDEX_MAX = 100;
       private static final long SLEEP_DELAY = 500;
       private JButton startProcessingBtn = new JButton("Start");
       private JToggleButton pauseProcessingBtn = new JToggleButton("Pause");
       private JButton endProcessingBtn = new JButton("End");
       private boolean abortProcessing;
       private boolean pauseProcessing;
       private MySwingWorker mySwingWorker;
    
       public SimplePauseProcessing() {
          add(startProcessingBtn);
          add(pauseProcessingBtn);
          add(endProcessingBtn);
    
          pauseProcessingBtn.setEnabled(false);
          endProcessingBtn.setEnabled(false);
    
          startProcessingBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                startProcessingActionPerformed();
             }
          });
          endProcessingBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                endProcessingActionPerformed();
             }
          });
          pauseProcessingBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                pauseProcessingActionPerformed();
             }
          });
       }
    
       private void startProcessingActionPerformed() {
          abortProcessing = false;
          pauseProcessing = false;
          
          new MySwingWorker().execute();
          startProcessingBtn.setEnabled(false);
          endProcessingBtn.setEnabled(true);
          pauseProcessingBtn.setEnabled(true);
          pauseProcessingBtn.setSelected(false);
       }
    
       private void endProcessingActionPerformed() {
          abortProcessing = true;
    
          startProcessingBtn.setEnabled(true);
          endProcessingBtn.setEnabled(false);
          pauseProcessingBtn.setEnabled(false);
          pauseProcessingBtn.setSelected(false);
       }
    
       private void pauseProcessingActionPerformed() {
          pauseProcessing = pauseProcessingBtn.isSelected();
       }
       
       public boolean backGroundTask() {
          for (int i = 0; i < INDEX_MAX; i++) {
             System.out.println("Index: " + i);
             try {
                Thread.sleep(SLEEP_DELAY);
             } catch (InterruptedException e) {}
             
             if (abortProcessing) {
                return false;
             }
             
             while (pauseProcessing) {
                try {
                   Thread.sleep(50);  // [color="red"]***** need this !!! *****[/color]
                   if (abortProcessing) {
                      return false;
                   }
                } catch (InterruptedException e) {}
             }
          }
          return true;
       }
       
       private class MySwingWorker extends SwingWorker<Boolean, Void> {
    
          @Override
          protected Boolean doInBackground() throws Exception {
             return backGroundTask();
          }
          
          @Override
          protected void done() {
             startProcessingBtn.setEnabled(true);
             pauseProcessingBtn.setEnabled(false);
             endProcessingBtn.setEnabled(false);
          }
          
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("SimplePauseProcessing");
          frame.getContentPane().add(new SimplePauseProcessing());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  5. #5
    Join Date
    Feb 2011
    Location
    Florida
    Posts
    60
    Rep Power
    0

    Default

    Ah did not think of that! That worked! Thanks a heap!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by madroadbiker View Post
    Ah did not think of that! That worked! Thanks a heap!
    Glad it helped, and you're welcome!

Similar Threads

  1. SwingWorker
    By 3.14.TR in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-14-2011, 05:53 PM
  2. Using SwingWorker
    By viking90 in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 10:17 AM
  3. How to pause
    By ravjot28 in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-31-2010, 07:09 AM
  4. Help with Pause
    By trill in forum Java Applets
    Replies: 2
    Last Post: 09-28-2007, 09:50 PM
  5. swingworker
    By musiigedeo in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 01: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
  •