From aa78093cc91a7fc093d56f33c2fe6a15a50ec2c3 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Fri, 27 May 2016 15:09:44 +0300 Subject: [PATCH] ProgressManager: allow to run tasks from non-EDT thread --- .../progress/impl/CoreProgressManager.java | 36 +++++++++++++++++-- plugins/git4idea/src/git4idea/GitVcs.java | 2 +- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/platform/core-impl/src/com/intellij/openapi/progress/impl/CoreProgressManager.java b/platform/core-impl/src/com/intellij/openapi/progress/impl/CoreProgressManager.java index 0ae628f1a815..f0e1e9d4cd1b 100644 --- a/platform/core-impl/src/com/intellij/openapi/progress/impl/CoreProgressManager.java +++ b/platform/core-impl/src/com/intellij/openapi/progress/impl/CoreProgressManager.java @@ -317,19 +317,49 @@ public class CoreProgressManager extends ProgressManager implements Disposable { } } else if (task.isModal()) { - runProcessWithProgressSynchronously(task.asModal(), null); + runSynchronously(task.asModal()); } else { final Task.Backgroundable backgroundable = task.asBackgroundable(); if (backgroundable.isConditionalModal() && !backgroundable.shouldStartInBackground()) { - runProcessWithProgressSynchronously(task, null); + runSynchronously(task); } else { - runProcessWithProgressAsynchronously(backgroundable); + runAsynchronously(backgroundable); } } } + private void runSynchronously(@NotNull final Task task) { + Runnable runnable = new Runnable() { + @Override + public void run() { + runProcessWithProgressSynchronously(task, null); + } + }; + if (ApplicationManager.getApplication().isDispatchThread()) { + runnable.run(); + } + else { + ApplicationManager.getApplication().invokeAndWait(runnable, ModalityState.NON_MODAL); + } + } + + private void runAsynchronously(@NotNull final Task.Backgroundable task) { + Runnable runnable = new Runnable() { + @Override + public void run() { + runProcessWithProgressAsynchronously(task); + } + }; + if (ApplicationManager.getApplication().isDispatchThread()) { + runnable.run(); + } + else { + ApplicationManager.getApplication().invokeLater(runnable, ModalityState.NON_MODAL); + } + } + @NotNull public Future runProcessWithProgressAsynchronously(@NotNull Task.Backgroundable task) { return runProcessWithProgressAsynchronously(task, new EmptyProgressIndicator(), null); diff --git a/plugins/git4idea/src/git4idea/GitVcs.java b/plugins/git4idea/src/git4idea/GitVcs.java index eed4c7bb4549..5de354f4d328 100644 --- a/plugins/git4idea/src/git4idea/GitVcs.java +++ b/plugins/git4idea/src/git4idea/GitVcs.java @@ -168,7 +168,7 @@ public class GitVcs extends AbstractVcs { * @param task the task to run */ public static void runInBackground(Task.Backgroundable task) { - UIUtil.invokeLaterIfNeeded(() -> task.queue()); + task.queue(); } @Override