Solved

Wait for task doInBackground() to finish

Posted on 2011-03-19
14
1,237 Views
Last Modified: 2012-05-11
How do I wait for ProgressTask to finish before procedding?
(see ???)

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;

public class ProgressPanel extends JPanel {

	private JProgressBar jProgressBar;

	/**
	 * constructor
	 * 
	 */
	public ProgressPanel() {
		
		this.setLayout(new BorderLayout());
		jProgressBar = new JProgressBar();
		JButton button = new JButton("Go");
		this.add(button, BorderLayout.LINE_START);
		this.add(jProgressBar, BorderLayout.LINE_END);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jProgressBar.setIndeterminate(true);
				new ProgressTask().execute(); // doInBackground()
				
				// how to wait for ProgressTask to finish???
				System.out.println ("Do other stuff, but wait for task to finish");
			}
		});
	}


	/**
	 * 
	 */
	private void doStuff() {
	    // do something	
		System.out.println("ProgressPanel.doStuff()");
	}
	
	/**
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (final Exception e) {
			e.printStackTrace();
		}

		try {
			JFrame frame = new JFrame("This is a test");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			ProgressPanel progressPanel = new ProgressPanel();
			frame.add(progressPanel);
			frame.setSize(300, 100);
			frame.setVisible(true);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * constructor
	 */
	private class ProgressTask extends SwingWorker<Void, Void> {
		
		public Void doInBackground() {
			
			doStuff();				

			return null;
		}

		public void done() {
			jProgressBar.setIndeterminate(false);
		}
	}
}

Open in new window

0
Comment
Question by:allelopath
  • 7
  • 4
  • 3
14 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35172583
I guess you can have some instance boolean variable in your
ProgresPanel class and after doStuff() executes in ProgressTask in doItInBackground()
you can toggle this variable - and then you can check it
at any point you want to do something dependent on
the completeion of that process.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35172643
done() will be called when it's done

>>before procedding?

If you mean you don't want the user to do anything until it's finished, disable the gui beforehand and re-enable in done()
0
 
LVL 1

Author Comment

by:allelopath
ID: 35172684
I tried the boolean, but doesn't work, maybe I am doing something wrong:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;

public class ProgressPanel extends JPanel {

	private JProgressBar jProgressBar;

	private boolean done;
	
	/**
	 * constructor
	 * 
	 */
	public ProgressPanel() {
		
		done = false;
		
		this.setLayout(new BorderLayout());
		jProgressBar = new JProgressBar();
		JButton button = new JButton("Go");
		this.add(button, BorderLayout.LINE_START);
		this.add(jProgressBar, BorderLayout.LINE_END);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jProgressBar.setIndeterminate(true);
				new ProgressTask().execute(); // doInBackground()
				
				while (! done) {
					System.out.println ("done =" + done);
				} // wait
				
				// how to wait for ProgressTask to finish???
				System.out.println ("Do other stuff, but wait for task to finish");
			}
		});
	}


	/**
	 * 
	 */
	private void doStuff() {
	    // do something	
		System.out.println("ProgressPanel.doStuff()");
	}
	
	/**
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (final Exception e) {
			e.printStackTrace();
		}

		try {
			JFrame frame = new JFrame("This is a test");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			ProgressPanel progressPanel = new ProgressPanel();
			frame.add(progressPanel);
			frame.setSize(300, 100);
			frame.setVisible(true);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * constructor
	 */
	private class ProgressTask extends SwingWorker<Void, Void> {
		
		public Void doInBackground() {
			
			System.out.println("ProgressTask.doInBackground(): ");
			
			doStuff();				

			return null;
		}

		public void done() {
			System.out.println("ProgressTask.done() ");
			done = true;
			
			jProgressBar.setIndeterminate(false);
		}
	}
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35172694
That's not going to help you - you'd require another thread or at least a loop to check that. Much better to take a similar approach to the one i mentioned
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35172706
But that's what the author did - placed it in the done() method - it should work

In what sense it is not working please explain
0
 
LVL 1

Author Comment

by:allelopath
ID: 35173017
It never gets past the while(!done) loop, ie done is never set to true.
It is set to true in the done() method, but that method is never reached.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35173027
allelopath - what do you (not) want the user to do?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 47

Expert Comment

by:for_yan
ID: 35173045
Still try to set this varoiable immediatly after doStouff() in the same method; perhaps it will work
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 35173053
Have a look at the following and watch the button after you click it:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.UIManager;


public class ProgressPanel extends JPanel {
    JProgressBar jProgressBar;
    JButton bnGo;

    public ProgressPanel() {
	this.setLayout(new BorderLayout());
	jProgressBar = new JProgressBar();
	bnGo = new JButton("Go");
	this.add(bnGo, BorderLayout.LINE_START);
	this.add(jProgressBar, BorderLayout.LINE_END);

	bnGo.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		jProgressBar.setIndeterminate(true);
		bnGo.setEnabled(false);
		new ProgressTask().execute();
	    }
	});
    }

    /**
     *
     * @param indeterminate
     */
    public void setProgressIndeterminate(boolean indeterminate) {
	if (indeterminate) {
	    System.out.println("ProgressPanel.setProgressIndeterminate(): true");
	    jProgressBar.setIndeterminate(true);
	} else {
	    System.out.println(
		    "ProgressPanel.setProgressIndeterminate(): false");
	    jProgressBar.setIndeterminate(false);
	    jProgressBar.setValue(0);
	}

	this.updateUI();
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
	try {
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (final Exception e) {
	    e.printStackTrace();
	}

	try {
	    JFrame frame = new JFrame("This is a test");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    ProgressPanel progressPanel = new ProgressPanel();
	    frame.add(progressPanel);
	    frame.setSize(300, 100);
	    frame.setVisible(true);
	} catch (final Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * constructor
     */
    private class ProgressTask extends SwingWorker<Void, Void> {
	public Void doInBackground() {
	    try {
		System.out.println("Sleeping for two secs...");
		Thread.sleep(2000);
	    } catch (InterruptedException e) {
		/* ignore */
	    }

	    return null;
	}

	public void done() {
	    jProgressBar.setIndeterminate(false);
	    bnGo.setEnabled(true);
	}
    }
}

Open in new window

0
 
LVL 1

Author Comment

by:allelopath
ID: 35173134
So when you said disable the gui, you meant disable the button. Makes more sense now.
Your code is a step in the right direction, but my original question remains.
If I add the done boolean, setting it to true in the done() method, it never gets past the while loop because done() is not called.

Keep in my that this is a test program, in the 'real' application, I want to wait for the task to finish before moving on to do other stuff (after the while loop)

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35173152
>> in the 'real' application, I want to wait for the task to finish before moving on to do other stuff (after the while loop)

All you need do is to disable whatever part(s) of the gui would allow the user to move on

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35173179
The theory is not really important, as the approach is clear (see above) but if you want it, what you're doing is effectively blocking the EDT so that done can never be called on it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35173370
:)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35173515
@alleopath
So can you explain me if it indeed executes done() method after it finishes doInBackground()?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now