SWTChart not redraw when it is inside a loop

81 Views Asked by At

If I have the following loop:

Chart plot = new Chart(composite, SWT.NONE);
ISeriesSet seriesSet = plot.getSeriesSet();
ISeries series = seriesSet.createSeries(SeriesType.LINE, "Test");

for(int i=0; i<1000; i++) {
    points = ...
    series.setYSeries(points);
}

The chart is not refreshed quickly enough, even if I add a delay within the loop. I had to minimize the window and then maximize it to see the changes, in other words, it is not refreshed after every loop, I tried to force a refresh by adding this lines inside the loop:

plot.redraw();
plot.layout(true, true);

But seems like it is refreshed only once at the end of the loop. Is there a best practice I should be following when having to quickly refreshing the chart?

1

There are 1 best solutions below

0
On BEST ANSWER

This code is running in the UI thread and will block UI updates until you return to the main Display.readAndDispatch loop. Code in the main UI thread must execute quickly and return to the main loop as soon as possible. The UI is only updated when readAndDispatch is executed.

So you can't use a simple for loop in the UI thread to update the chart.

One way to do this is to use Display.timerExec to run a single step after a delay.

Or you can start a background thread and put your loop there. From a background thread you must call Display.syncExec to run the code which updates the UI.