diff --git a/platform/core-api/src/com/intellij/openapi/progress/ProgressManager.java b/platform/core-api/src/com/intellij/openapi/progress/ProgressManager.java index f823b139b900..f5b06987271b 100644 --- a/platform/core-api/src/com/intellij/openapi/progress/ProgressManager.java +++ b/platform/core-api/src/com/intellij/openapi/progress/ProgressManager.java @@ -207,6 +207,10 @@ public abstract class ProgressManager extends ProgressIndicatorProvider { getInstance().indicatorCanceled(indicator); } + /** + * @throws ProcessCanceledException if the progress indicator associated with the current thread has been canceled. + * @see ProgressIndicator#checkCanceled() + */ @SuppressWarnings("MethodOverridesStaticMethodOfSuperclass") public static void checkCanceled() throws ProcessCanceledException { ProgressManager instance = ourInstance; diff --git a/platform/platform-impl/src/com/intellij/openapi/progress/util/ProgressIndicatorUtils.java b/platform/platform-impl/src/com/intellij/openapi/progress/util/ProgressIndicatorUtils.java index f57fecf91825..6d996b546c70 100644 --- a/platform/platform-impl/src/com/intellij/openapi/progress/util/ProgressIndicatorUtils.java +++ b/platform/platform-impl/src/com/intellij/openapi/progress/util/ProgressIndicatorUtils.java @@ -1,6 +1,7 @@ // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.progress.util; +import com.intellij.concurrency.SensitiveProgressWrapper; import com.intellij.openapi.Disposable; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationListener; @@ -11,6 +12,7 @@ import com.intellij.openapi.application.ex.ApplicationManagerEx; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.openapi.progress.ProgressIndicatorProvider; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.util.*; import com.intellij.util.concurrency.AppExecutorUtil; @@ -19,6 +21,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.ide.PooledThreadExecutor; import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Lock; /** @@ -249,16 +252,32 @@ public class ProgressIndicatorUtils { application.invokeAndWait(EmptyRunnable.INSTANCE, ModalityState.any()); } + /** + * Run the given computation with its execution time restricted to the given amount of time in milliseconds.
+ * + * Internally, it creates a new {@link ProgressIndicator}, runs the computation with that indicator and cancels it after the the timeout. + * The computation should call {@link ProgressManager#checkCanceled()} frequently enough, so that after the timeout has been exceeded + * it can stop the execution by throwing {@link ProcessCanceledException}, which will be caught by this {@code withTimeout}. + * + * If a {@link ProcessCanceledException} happens due to any other reason (e.g. a thread's progress indicator got canceled), + * it'll be thrown out of this method. + * @return the computation result or {@code null} if timeout has been exceeded. + */ @Nullable - public static