Solved

Wait for task doInBackground() to finish

Posted on 2011-03-19
14
1,268 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 45
JList custom Cell Renderer refresh 15 57
VB Script to add site to Java Exception List 4 79
tomcat startup error 5 66
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

832 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