Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Threading and displaying exceptions in dialog confusion
Threading and displaying exceptions in dialog confusion [message #466680] Fri, 13 January 2006 04:57 Go to next message
mp is currently offline mp
Messages: 8
Registered: July 2009
Junior Member
First my apologies for this noob question but I'm new to threading and
until now did not go further then using some snippets..

I'm running a database procedure in a separate thread. When everything
runs fine I see a box with a progressbase till action is finished.
Now I look into a way to display the exception in a new form when
something goes wrong.

However this does not work. When an error occurs the dialog with the
progressbar continues being displayed and never ends.
and application runs into Exception in thread "Thread-0"
org.eclipse.swt.SWTException: Invalid thread access.

How can I solve this?



public int open() {
createContents();
Display display = getParent().getDisplay();
Rectangle parentSize = getParent().getBounds();
Rectangle mySize = shell.getBounds();
int locationX, locationY;
locationX = (parentSize.width - mySize.width)/2+parentSize.x;
locationY = (parentSize.height - mySize.height)/2+parentSize.y;
shell.setLocation(new Point(locationX, locationY));
shell.open();
shell.layout();
oraSettings = OracleSettings.instance();
conn = oraSettings.getOracleConnection();
new databaseOperation(display).start();
System.out.println("Finished");
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}


protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(241, 119);
shell.setText("Working - Please wait");

progressBar = new ProgressBar(shell, SWT.INDETERMINATE);
progressBar.setBounds(14, 32, 206, 19);

final Label testLabel = new Label(shell, SWT.NONE);
testLabel.setText("Busy...");
testLabel.setBounds(14, 8, 209, 17);

final Button cancelButton = new Button(shell, SWT.NONE);
cancelButton.setText("cancel");
cancelButton.setBounds(78, 57, 78, 26);
}


private void databaseAction() {
...
try {
...
}
catch (SQLException e) {
// In this class I try to open a dialog to display the stack error.
databaseUtil.showException(shell, e);
}
finally {
...
}
}


class databaseOperation extends Thread {
private Display display;

public databaseOperation(Display display) {
this.display = display;
}

public void run () {
databaseAction();
display.asyncExec(new Runnable() {
public void run() {
shell.close();
}
});
}
}
Re: Threading and displaying exceptions in dialog confusion [message #466681 is a reply to message #466680] Fri, 13 January 2006 05:10 Go to previous message
Yves Harms is currently offline Yves Harms
Messages: 80
Registered: July 2009
Member
ShowException must be run in the display thread (use the displays
synExec or asyncExec).


> First my apologies for this noob question but I'm new to threading and
> until now did not go further then using some snippets..
>
> I'm running a database procedure in a separate thread. When everything
> runs fine I see a box with a progressbase till action is finished.
> Now I look into a way to display the exception in a new form when
> something goes wrong.
>
> However this does not work. When an error occurs the dialog with the
> progressbar continues being displayed and never ends.
> and application runs into Exception in thread "Thread-0"
> org.eclipse.swt.SWTException: Invalid thread access.
>
> How can I solve this?
>
>
>
> public int open() {
> createContents();
> Display display = getParent().getDisplay();
> Rectangle parentSize = getParent().getBounds();
> Rectangle mySize = shell.getBounds();
> int locationX, locationY;
> locationX = (parentSize.width - mySize.width)/2+parentSize.x;
> locationY = (parentSize.height - mySize.height)/2+parentSize.y;
> shell.setLocation(new Point(locationX, locationY));
> shell.open();
> shell.layout();
> oraSettings = OracleSettings.instance();
> conn = oraSettings.getOracleConnection();
> new databaseOperation(display).start();
> System.out.println("Finished");
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> return result;
> }
>
>
> protected void createContents() {
> shell = new Shell(getParent(), SWT.DIALOG_TRIM |
> SWT.APPLICATION_MODAL);
> shell.setSize(241, 119);
> shell.setText("Working - Please wait");
>
> progressBar = new ProgressBar(shell, SWT.INDETERMINATE);
> progressBar.setBounds(14, 32, 206, 19);
>
> final Label testLabel = new Label(shell, SWT.NONE);
> testLabel.setText("Busy...");
> testLabel.setBounds(14, 8, 209, 17);
>
> final Button cancelButton = new Button(shell, SWT.NONE);
> cancelButton.setText("cancel");
> cancelButton.setBounds(78, 57, 78, 26);
> }
>
>
> private void databaseAction() {
> ...
> try {
> ...
> }
> catch (SQLException e) {
> // In this class I try to open a dialog to display the stack
> error.
> databaseUtil.showException(shell, e);
> }
> finally {
> ...
> }
> }
>
>
> class databaseOperation extends Thread {
> private Display display;
>
> public databaseOperation(Display display) {
> this.display = display;
> }
>
> public void run () {
> databaseAction();
> display.asyncExec(new Runnable() {
> public void run() {
> shell.close();
> }
> });
> }
> }
>
>
Previous Topic:How to detect refresh event in Browser
Next Topic:Hi,could you pls help me have a look at this question?
Goto Forum:
  


Current Time: Wed Jul 30 13:13:14 EDT 2014

Powered by FUDForum. Page generated in 0.01661 seconds