Skip to content

Commit 0da1f60

Browse files
fedejeanneCopilot
andcommitted
Honor 'Always run in background' in IProgressService.run() #4202
Fixes #4202 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c402a17 commit 0da1f60

2 files changed

Lines changed: 50 additions & 29 deletions

File tree

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/progress/ProgressManager.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,22 +1075,28 @@ public void showInDialog(Shell shell, Job job) {
10751075
@Override
10761076
public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable)
10771077
throws InvocationTargetException, InterruptedException {
1078+
if (shouldRunInBackground()) {
1079+
// The "Always run in background" preference must suppress the modal
1080+
// progress dialog for every fork/cancelable combination, not just the
1081+
// backward compatible (fork==false || cancelable==false) path below.
1082+
final ProgressMonitorJobsDialog dialog = new ProgressMonitorJobsDialog(
1083+
ProgressManagerUtil.getDefaultParent());
1084+
dialog.setOpenOnRun(false);
1085+
dialog.run(fork, cancelable, runnable);
1086+
return;
1087+
}
1088+
10781089
if (!fork || !cancelable) {
10791090
// Backward compatible code.
10801091
final ProgressMonitorJobsDialog dialog = new ProgressMonitorJobsDialog(
10811092
ProgressManagerUtil.getDefaultParent());
1082-
if (shouldRunInBackground()) {
1083-
dialog.setOpenOnRun(false);
1093+
Job showDialogJob = scheduleProgressMonitorJob(dialog);
1094+
try {
10841095
dialog.run(fork, cancelable, runnable);
1085-
} else {
1086-
Job showDialogJob = scheduleProgressMonitorJob(dialog);
1087-
try {
1088-
dialog.run(fork, cancelable, runnable);
1089-
} finally {
1090-
// In case the dialog hasn't popped up yet, cancel it so it doesn't pop up after
1091-
// the operation finishes or unwinds with an exception.
1092-
showDialogJob.cancel();
1093-
}
1096+
} finally {
1097+
// In case the dialog hasn't popped up yet, cancel it so it doesn't pop up after
1098+
// the operation finishes or unwinds with an exception.
1099+
showDialogJob.cancel();
10941100
}
10951101
return;
10961102
}

examples/org.eclipse.ui.examples.job/src/org/eclipse/ui/examples/jobs/views/ProgressServiceView.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)