Solved

Wait for task doInBackground() to finish

Posted on 2011-03-19
14
1,252 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java and GPO 11 66
Groovy:unable to resolve class error 2 66
starter POM and spring-boot-starter,  spring-boot-web 2 40
rhino JavaScript import, load 25 66
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

914 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

13 Experts available now in Live!

Get 1:1 Help Now