@@ -44,7 +44,11 @@ public class ProgressServiceView extends ViewPart {
4444
4545 private static final int SLEEP_STEP_MS = 100 ;
4646
47- private final ExecutorService nonUiExecutor = Executors .newSingleThreadExecutor ();
47+ private final ExecutorService nonUiExecutor = Executors .newSingleThreadExecutor (r -> {
48+ Thread t = new Thread (r , "ProgressServiceView-nonUI" ); //$NON-NLS-1$
49+ t .setDaemon (true );
50+ return t ;
51+ });
4852
4953 private Button backgroundPrefField ;
5054 private IPropertyChangeListener prefListener ;
@@ -88,7 +92,14 @@ private void createPreferenceGroup(Composite parent) {
8892
8993 prefListener = event -> {
9094 if (IPreferenceConstants .RUN_IN_BACKGROUND .equals (event .getProperty ())) {
91- Display .getDefault ().asyncExec (() -> {
95+ if (backgroundPrefField .isDisposed ()) {
96+ return ;
97+ }
98+ Display display = backgroundPrefField .getDisplay ();
99+ if (display .isDisposed ()) {
100+ return ;
101+ }
102+ display .asyncExec (() -> {
92103 if (!backgroundPrefField .isDisposed ()) {
93104 backgroundPrefField
94105 .setSelection (getPreferenceStore ().getBoolean (IPreferenceConstants .RUN_IN_BACKGROUND ));
@@ -188,25 +199,29 @@ private IRunnableWithProgress createSleepRunnable(long durationMillis, boolean p
188199 return monitor -> {
189200 int ticks = (int ) Math .max (1 , durationMillis / SLEEP_STEP_MS );
190201 monitor .beginTask ("Simulated long-running operation" , ticks ); //$NON-NLS-1$
191- for (int i = 0 ; i < ticks ; i ++) {
192- if (monitor .isCanceled ()) {
193- return ;
194- }
195- try {
196- Thread .sleep (SLEEP_STEP_MS );
197- } catch (InterruptedException e ) {
198- Thread .currentThread ().interrupt ();
199- return ;
200- }
201- if (pumpEvents ) {
202- Display display = Display .getCurrent ();
203- if (display != null ) {
204- while (display .readAndDispatch ()) {
205- // drain pending UI events so the heartbeat keeps ticking
202+ try {
203+ for (int i = 0 ; i < ticks ; i ++) {
204+ if (monitor .isCanceled ()) {
205+ return ;
206+ }
207+ try {
208+ Thread .sleep (SLEEP_STEP_MS );
209+ } catch (InterruptedException e ) {
210+ Thread .currentThread ().interrupt ();
211+ return ;
212+ }
213+ if (pumpEvents ) {
214+ Display display = Display .getCurrent ();
215+ if (display != null ) {
216+ while (display .readAndDispatch ()) {
217+ // drain pending UI events so the heartbeat keeps ticking
218+ }
206219 }
207220 }
221+ monitor .worked (1 );
208222 }
209- monitor .worked (1 );
223+ } finally {
224+ monitor .done ();
210225 }
211226 };
212227 }
0 commit comments