Search in sources :

Example 1 with CalledInAny

use of org.jetbrains.annotations.CalledInAny in project intellij-community by JetBrains.

the class BackgroundTaskUtil method executeOnPooledThread.

@NotNull
@CalledInAny
public static ProgressIndicator executeOnPooledThread(@NotNull Consumer<ProgressIndicator> task, @NotNull Disposable parent, @NotNull ModalityState modalityState) {
    ProgressIndicator indicator = new EmptyProgressIndicator(modalityState);
    Disposable disposable = new Disposable() {

        @Override
        public void dispose() {
            if (indicator.isRunning())
                indicator.cancel();
        }
    };
    Disposer.register(parent, disposable);
    indicator.start();
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        ProgressManager.getInstance().executeProcessUnderProgress(() -> {
            try {
                task.consume(indicator);
            } finally {
                indicator.stop();
                Disposer.dispose(disposable);
            }
        }, indicator);
    });
    return indicator;
}
Also used : Disposable(com.intellij.openapi.Disposable) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) CalledInAny(org.jetbrains.annotations.CalledInAny) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with CalledInAny

use of org.jetbrains.annotations.CalledInAny in project intellij-community by JetBrains.

the class BackgroundTaskUtil method computeInBackgroundAndTryWait.

/**
   * Compute value in background and try wait for its completion.
   * <ul>
   * <li> If the computation is fast, return computed value synchronously. Callback will not be called in this case.
   * <li> If the computation is slow, return <tt>null</tt>. When the computation is completed, pass the value to the callback.
   * </ul>
   * Callback will be executed on the same thread as the background task.
   */
@NotNull
@CalledInAny
public static <T> Pair<T, ProgressIndicator> computeInBackgroundAndTryWait(@NotNull Function<ProgressIndicator, T> task, @NotNull PairConsumer<T, ProgressIndicator> asyncCallback, @NotNull ModalityState modality, long waitMillis) {
    ProgressIndicator indicator = new EmptyProgressIndicator(modality);
    Helper<T> helper = new Helper<>();
    indicator.start();
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        ProgressManager.getInstance().executeProcessUnderProgress(() -> {
            try {
                T result = task.fun(indicator);
                if (!helper.setResult(result)) {
                    asyncCallback.consume(result, indicator);
                }
            } finally {
                indicator.stop();
            }
        }, indicator);
    });
    T result = null;
    if (helper.await(waitMillis)) {
        result = helper.getResult();
    }
    return Pair.create(result, indicator);
}
Also used : EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) CalledInAny(org.jetbrains.annotations.CalledInAny) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 CalledInAny (org.jetbrains.annotations.CalledInAny)2 NotNull (org.jetbrains.annotations.NotNull)2 Disposable (com.intellij.openapi.Disposable)1