Program with (a)syncExec() hangs after Thread.join() [message #451821] |
Wed, 09 March 2005 15:38 |
Robert Messages: 45 Registered: July 2009 |
Member |
|
|
Hi!
I am working on a straightforward program for downloading files from the
web. I'd like to display a ProgressBar in the meantime. As soon as the
download completes, I want to continue working on the contents of the
file. So far so good, and the core of the program has been implemented and
seems to work. However, I can't figure out if the download has been
finished. I figured a Thread.join() would do the trick (wait for the
download thread to complete/die), but this causes my program to hang.
Without the join(), it works like a charm. I am obviously doing something
wrong with the threading (syncExec/asyncExec/join), but I don't understand
the problem.
Could anyone provide some insight?
Thanks!
Robert
My source code (simplified for sake of clarity):
----------------------------
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class Testing {
public static void main ( String args[] ) {
final Display display = new Display();
Shell shell = new Shell(display);
final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
bar.setSize(400, 32);
final Button button = new Button(shell, SWT.PUSH);
button.setText("Go!");
button.setBounds(10, 60, 120, 28);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected ( SelectionEvent event ) {
try {
bar.setMinimum(0);
bar.setMaximum(10000);
button.setEnabled(false);
Thread thread = new Thread() {
public void run ( ) {
final int[] completed = {0};
while ( true ) {
completed[0]++;
if ( completed[0] == 10000 ) break;
display.syncExec(new Runnable() {
public void run ( ) {
bar.setSelection(completed[0]);
}
});
}
}
};
thread.start();
//thread.join(); // <-- uncomment!
// want to continue here
} catch ( Exception e ) {
e.printStackTrace();
} finally {
button.setEnabled(true);
button.setText("Done!");
}
}
});
shell.setSize(640, 480);
shell.open();
while ( !shell.isDisposed() ) {
if ( !display.readAndDispatch() ) {
display.sleep();
}
}
display.dispose();
}
};
|
|
|
Re: Program with (a)syncExec() hangs after Thread.join() [message #451830 is a reply to message #451821] |
Wed, 09 March 2005 16:19 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
Your thread.join is done from the UI thread (all widget events come in on
the UI thread). So effectively, you are saying that the UI thread should
stop (i.e. your GUI should hang) until the work done in your thread is
completed. And the work in your thread won't complete until it runs some
code in the UI thread. Deadlock. Suspending the UI thread defeats the
purpose of doing your work in another thread in the first place.
Why not just execute
button.setEnabled(true);
button.setText("Done!");
in the display.syncExec call from thread?
"Robert" <robert_x20032000@yahoo.com> wrote in message
news:d0n59k$8be$1@www.eclipse.org...
> Hi!
>
> I am working on a straightforward program for downloading files from the
> web. I'd like to display a ProgressBar in the meantime. As soon as the
> download completes, I want to continue working on the contents of the
> file. So far so good, and the core of the program has been implemented and
> seems to work. However, I can't figure out if the download has been
> finished. I figured a Thread.join() would do the trick (wait for the
> download thread to complete/die), but this causes my program to hang.
> Without the join(), it works like a charm. I am obviously doing something
> wrong with the threading (syncExec/asyncExec/join), but I don't understand
> the problem.
>
> Could anyone provide some insight?
>
> Thanks!
> Robert
>
> My source code (simplified for sake of clarity):
>
> ----------------------------
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.SelectionAdapter;
> import org.eclipse.swt.events.SelectionEvent;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.ProgressBar;
> import org.eclipse.swt.widgets.Shell;
>
> public class Testing {
>
> public static void main ( String args[] ) {
>
> final Display display = new Display();
> Shell shell = new Shell(display);
>
> final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
> bar.setSize(400, 32);
>
> final Button button = new Button(shell, SWT.PUSH);
> button.setText("Go!");
> button.setBounds(10, 60, 120, 28);
> button.addSelectionListener(new SelectionAdapter() {
> public void widgetSelected ( SelectionEvent event ) {
> try {
> bar.setMinimum(0);
> bar.setMaximum(10000);
> button.setEnabled(false);
> Thread thread = new Thread() {
> public void run ( ) {
> final int[] completed = {0};
> while ( true ) {
> completed[0]++;
> if ( completed[0] == 10000 ) break;
> display.syncExec(new Runnable() {
> public void run ( ) {
> bar.setSelection(completed[0]);
> }
> });
> }
> }
> };
> thread.start();
> //thread.join(); // <-- uncomment!
> // want to continue here
> } catch ( Exception e ) {
> e.printStackTrace();
> } finally {
> button.setEnabled(true);
> button.setText("Done!");
> }
> }
> });
>
> shell.setSize(640, 480);
> shell.open();
>
> while ( !shell.isDisposed() ) {
> if ( !display.readAndDispatch() ) {
> display.sleep();
> }
> }
> display.dispose();
> }
> };
>
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03737 seconds