Search code examples
javaasynchronousswtprogressmonitor

ProgressMonitorDialog and user interaction


I'm using a ProgressMonitorDialog with an IRunnableWithProgress to read a file in the background.

If an error occurs during this file processing (the data isn't what I'm expecting), I would like to ask the user if s/he wants to continue with the next line.

The problem is now that in order to ask the user if they want to continue, I'd have to show a dialog. Showing a dialog from a non-UI thread involves using Display.asyncExec() or Display.syncExec(). In order to return the result (user decision) to the background thread I'd have to use a callback.

Now, the problem is, that when I get the result in a callback in the background thread, how can I continue reading the file? Or, in other words, how can I pause the execution of the background thread until the feedback returns and then continue it?

I'm open to suggestions and willing to restructure my environment to accommodate this behaviour.


Solution

  • Dislay.syncExec blocks the thread you call it from so you can do something like:

    final int[] result = new int[] {0};
    
    display.syncExec(new Runnable() {
        public void run() {
            Shell shell = display.getActiveShell();
    
            MessageDialog dialog = .... your message dialog
    
            result[0] = dialog.open();
        }
    });
    
    ... dialog return code in result[0]
    

    (heavily adapted from code in org.eclipse.equinox.internal.p2.ui.ValidationDialogServiceUI)