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;
}
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);
}
Aggregations