1

I'm using Eclipse RAP and have the following use case:

  • enter search text in a Text field and start search
  • change a Label text to "searching...."
  • do actual search (async) and display result in a Table

The problem, even though the label should change to "searching...." before the actual search is started, it is changed to "searching...." after the search is done.

What I'm looking for is a way to push/force/update the current UI state to the client after the label changed, prior to searching:

  • enter search text in a Text field and start search
  • change a Label text to "searching...."
  • push current UI state to client
  • do actual search (async) and display result in a Table

Here some sample code:

    Label statusLabel = new Label(parent, SWT.NONE);
    Text searchText = new Text(parent, SWT.SEARCH);
    searchText.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            // change label
            statusLabel.setText("searching...");

            // HERE force client update

            // start searching
            Display.getCurrent().asyncExec(new Runnable() {
                @Override
                public void run() {
                    // do actual search
                }
            });
        }
    });
flavio.donze
  • 7,432
  • 9
  • 58
  • 91

1 Answers1

2

asyncExec still runs the code in the UI thread it just delays it slightly until the next call to Display.readAndDispatch. So your search code will still block and isn't allowing the label to update.

You need to actually run the search in a separate thread.

asyncExec is intended to be used in a background thread to run a small amount of code in the UI thread when possible. (You need to use Display.getDefault() rather than Display.getCurrent() in a background thread).

So in your background thread you do something like:

while (more to do)
 {
   .... do a step of the search

   Display.getDefault().asyncExec(.... UI update code ....);
 }
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • If I run the search in a separated thread, I will not be able to access the UI right? e.g. updating status Label, update Result Table. I would run into an "Invalid thread access" Exception, I guess? – flavio.donze Jun 21 '17 at 07:57
  • You use `asyncExec` (or `syncExec`) in the background thread to run your UI update code - that is what it is for. – greg-449 Jun 21 '17 at 08:36
  • Display.getDefault() and Display.getCurrent() were both null in the thread, I had to store it as variable before starting the thread. – flavio.donze Jun 21 '17 at 14:17