use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.
the class CacheUpdateRunner method processSomeFilesWhileUserIsInactive.
private static boolean processSomeFilesWhileUserIsInactive(@NotNull FileContentQueue queue, @NotNull ProgressUpdater progressUpdater, final boolean processInReadAction, @NotNull Project project, @NotNull Consumer<FileContent> fileProcessor) {
final ProgressIndicatorBase innerIndicator = new ProgressIndicatorBase() {
@Override
protected boolean isCancelable() {
// the inner indicator must be always cancelable
return true;
}
};
final ApplicationAdapter canceller = new ApplicationAdapter() {
@Override
public void beforeWriteActionStart(@NotNull Object action) {
innerIndicator.cancel();
}
};
final Application application = ApplicationManager.getApplication();
application.invokeAndWait(() -> application.addApplicationListener(canceller), ModalityState.any());
final AtomicBoolean isFinished = new AtomicBoolean();
try {
int threadsCount = indexingThreadCount();
if (threadsCount == 1 || application.isWriteAccessAllowed()) {
Runnable process = new MyRunnable(innerIndicator, queue, isFinished, progressUpdater, processInReadAction, project, fileProcessor);
ProgressManager.getInstance().runProcess(process, innerIndicator);
} else {
AtomicBoolean[] finishedRefs = new AtomicBoolean[threadsCount];
Future<?>[] futures = new Future<?>[threadsCount];
for (int i = 0; i < threadsCount; i++) {
AtomicBoolean ref = new AtomicBoolean();
finishedRefs[i] = ref;
Runnable process = new MyRunnable(innerIndicator, queue, ref, progressUpdater, processInReadAction, project, fileProcessor);
futures[i] = application.executeOnPooledThread(process);
}
isFinished.set(waitForAll(finishedRefs, futures));
}
} finally {
application.removeApplicationListener(canceller);
}
return isFinished.get();
}
use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.
the class DumbServiceImpl method addTaskToQueue.
private boolean addTaskToQueue(@NotNull DumbModeTask task) {
if (!myQueuedEquivalences.add(task.getEquivalenceObject())) {
Disposer.dispose(task);
return false;
}
myProgresses.put(task, new ProgressIndicatorBase());
Disposer.register(task, () -> {
ApplicationManager.getApplication().assertIsDispatchThread();
myProgresses.remove(task);
});
myUpdatesQueue.addLast(task);
return true;
}
use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.
the class ProgressIndicatorTest method testProgressManagerCheckCanceledDoesNotDelegateToProgressIndicatorIfThereAreNoCanceledIndicators.
public void testProgressManagerCheckCanceledDoesNotDelegateToProgressIndicatorIfThereAreNoCanceledIndicators() throws Throwable {
final long warmupEnd = System.currentTimeMillis() + 1000;
final long end = warmupEnd + 10000;
checkCanceledCalled = false;
final ProgressIndicatorBase myIndicator = new ProgressIndicatorBase();
taskCanceled = taskSucceeded = false;
exception = null;
Future<?> future = ((ProgressManagerImpl) ProgressManager.getInstance()).runProcessWithProgressAsynchronously(new Task.Backgroundable(getProject(), "Xxx") {
@Override
public void run(@NotNull ProgressIndicator indicator) {
try {
assertFalse(ApplicationManager.getApplication().isDispatchThread());
assertSame(indicator, myIndicator);
while (System.currentTimeMillis() < end) {
ProgressManager.checkCanceled();
}
} catch (ProcessCanceledException e) {
exception = e;
checkCanceledCalled = true;
throw e;
} catch (RuntimeException | Error e) {
exception = e;
throw e;
}
}
@Override
public void onCancel() {
taskCanceled = true;
}
@Override
public void onSuccess() {
taskSucceeded = true;
}
}, myIndicator, null);
ApplicationManager.getApplication().assertIsDispatchThread();
while (!future.isDone()) {
if (System.currentTimeMillis() < warmupEnd) {
assertFalse(checkCanceledCalled);
} else {
myIndicator.cancel();
}
}
// invokeLater in runProcessWithProgressAsynchronously
UIUtil.dispatchAllInvocationEvents();
assertTrue(checkCanceledCalled);
assertFalse(taskSucceeded);
assertTrue(taskCanceled);
assertTrue(String.valueOf(exception), exception instanceof ProcessCanceledException);
}
use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.
the class ProgressIndicatorTest method testProgressIndicatorUtilsScheduleWithWriteActionPriority.
public void testProgressIndicatorUtilsScheduleWithWriteActionPriority() throws Throwable {
final AtomicBoolean insideReadAction = new AtomicBoolean();
final ProgressIndicatorBase indicator = new ProgressIndicatorBase();
ProgressIndicatorUtils.scheduleWithWriteActionPriority(indicator, new ReadTask() {
@Override
public void computeInReadAction(@NotNull ProgressIndicator indicator) {
insideReadAction.set(true);
while (true) {
ProgressManager.checkCanceled();
}
}
@Override
public void onCanceled(@NotNull ProgressIndicator indicator) {
}
});
UIUtil.dispatchAllInvocationEvents();
while (!insideReadAction.get()) {
}
ApplicationManager.getApplication().runWriteAction(() -> assertTrue(indicator.isCanceled()));
assertTrue(indicator.isCanceled());
}
use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.
the class ProgressIndicatorTest method testCheckCanceledHasStackFrame.
public void testCheckCanceledHasStackFrame() {
ProgressIndicator pib = new ProgressIndicatorBase();
pib.cancel();
try {
pib.checkCanceled();
fail("Please restore ProgressIndicatorBase.checkCanceled() check!");
} catch (ProcessCanceledException ex) {
boolean hasStackFrame = ex.getStackTrace().length != 0;
assertTrue("Should have stackframe", hasStackFrame);
}
}
Aggregations