Search in sources :

Example 11 with ProgressIndicatorBase

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) Future(java.util.concurrent.Future) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter) NotNull(org.jetbrains.annotations.NotNull) Application(com.intellij.openapi.application.Application)

Example 12 with ProgressIndicatorBase

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;
}
Also used : ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase)

Example 13 with ProgressIndicatorBase

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);
}
Also used : ReadTask(com.intellij.openapi.progress.util.ReadTask) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) DelegatingProgressIndicator(com.intellij.ide.util.DelegatingProgressIndicator) BombedProgressIndicator(com.intellij.testFramework.BombedProgressIndicator) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)

Example 14 with ProgressIndicatorBase

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) DelegatingProgressIndicator(com.intellij.ide.util.DelegatingProgressIndicator) BombedProgressIndicator(com.intellij.testFramework.BombedProgressIndicator) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator) ReadTask(com.intellij.openapi.progress.util.ReadTask)

Example 15 with ProgressIndicatorBase

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);
    }
}
Also used : ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) DelegatingProgressIndicator(com.intellij.ide.util.DelegatingProgressIndicator) BombedProgressIndicator(com.intellij.testFramework.BombedProgressIndicator) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)

Aggregations

ProgressIndicatorBase (com.intellij.openapi.progress.util.ProgressIndicatorBase)25 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)8 DaemonProgressIndicator (com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)6 DelegatingProgressIndicator (com.intellij.ide.util.DelegatingProgressIndicator)6 ReadTask (com.intellij.openapi.progress.util.ReadTask)6 BombedProgressIndicator (com.intellij.testFramework.BombedProgressIndicator)6 NotNull (org.jetbrains.annotations.NotNull)5 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ApplicationManager (com.intellij.openapi.application.ApplicationManager)3 ModalityState (com.intellij.openapi.application.ModalityState)3 Logger (com.intellij.openapi.diagnostic.Logger)3 Project (com.intellij.openapi.project.Project)3 Ref (com.intellij.openapi.util.Ref)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 CommonBundle (com.intellij.CommonBundle)2 com.intellij.find (com.intellij.find)2 ShowUsagesAction (com.intellij.find.actions.ShowUsagesAction)2 Disposable (com.intellij.openapi.Disposable)2 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)2