Repository: intellij-rust/intellij-rust
Issue: Exception while building project via build button

## Environment

* **IntelliJ Rust plugin version:** 0.2.120.2202-201
* **Rust toolchain version:** 1.42.0 (b8cedc004 2020-03-09) x86_64-apple-darwin
* **IDE name and version:** IntelliJ IDEA 2020.1 Ultimate Edition (IU-201.6668.121)
* **Operating system:** macOS 10.13.6

## Problem description
Build action produces the following exception:
<details><summary>Stacktrace</summary>

```
Access is allowed from event dispatch thread with IW lock only.

com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: EventQueue.isDispatchThread()=false Toolkit.getEventQueue()=com.intellij.ide.IdeEventQueue@16df7a60
Current thread: Thread[ApplicationImpl pooled thread 98,4,Idea Thread Group] 684347736
SystemEventQueueThread: Thread[AWT-EventQueue-0,6,Idea Thread Group] 1791319634
	at com.intellij.openapi.application.impl.ApplicationImpl.assertIsDispatchThread(ApplicationImpl.java:1057)
	at com.intellij.openapi.application.impl.ApplicationImpl.assertIsDispatchThread(ApplicationImpl.java:1039)
	at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.doRegisterToolWindow(ToolWindowManagerImpl.kt:938)
	at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.registerToolWindow(ToolWindowManagerImpl.kt:923)
	at com.intellij.execution.ui.RunContentManagerImpl.registerToolWindow(RunContentManagerImpl.kt:117)
	at com.intellij.execution.ui.RunContentManagerImpl.getOrCreateContentManagerForToolWindow(RunContentManagerImpl.kt:362)
	at com.intellij.execution.ui.RunContentManagerImpl.getContentManagerForRunner(RunContentManagerImpl.kt:347)
	at com.intellij.execution.ui.RunContentManagerImpl.getReuseContent(RunContentManagerImpl.kt:322)
	at com.intellij.execution.impl.ExecutionManagerImpl.doStartRunProfile(ExecutionManagerImpl.kt:182)
	at com.intellij.execution.impl.ExecutionManagerImpl.startRunProfile(ExecutionManagerImpl.kt:145)
	at org.rust.cargo.runconfig.RsDefaultProgramRunnerBase.execute(RsDefaultProgramRunnerBase.kt:38)
	at org.rust.cargo.runconfig.RsDefaultProgramRunnerBase.execute(RsDefaultProgramRunnerBase.kt:23)
	at com.intellij.execution.impl.ExecutionManagerImpl.executeConfiguration(ExecutionManagerImpl.kt:612)
	at com.intellij.execution.ProgramRunnerUtil.executeConfigurationAsync(ProgramRunnerUtil.java:60)
	at com.intellij.execution.ProgramRunnerUtil.executeConfiguration(ProgramRunnerUtil.java:43)
	at com.intellij.execution.ProgramRunnerUtil.executeConfiguration(ProgramRunnerUtil.java:134)
	at org.rust.cargo.toolchain.CargoCommandLineKt.run(CargoCommandLine.kt:148)
	at org.rust.cargo.toolchain.CargoCommandLineKt.run$default(CargoCommandLine.kt:138)
	at org.rust.cargo.runconfig.UtilsKt.buildProject(Utils.kt:69)
	at org.rust.cargo.runconfig.buildtool.CargoBuildTaskRunner.run(CargoBuildTaskRunner.kt:52)
	at com.intellij.task.ProjectTaskRunner.run(ProjectTaskRunner.java:143)
	at com.intellij.task.ProjectTaskRunner.run(ProjectTaskRunner.java:70)
	at com.intellij.task.impl.ProjectTaskManagerImpl.lambda$run$13(ProjectTaskManagerImpl.java:222)
	at com.intellij.util.RunnableCallable.call(RunnableCallable.java:20)
	at com.intellij.util.RunnableCallable.call(RunnableCallable.java:11)
	at com.intellij.openapi.application.impl.ApplicationImpl$1.call(ApplicationImpl.java:255)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
```
</details>

## Steps to reproduce
* Create new rust project
* Ensure that `Experimental Features > org.rust.cargo.build.tool.window` option is disabled
* Press build button

<!--
Please include as much of your codebase as needed to reproduce the error.
If the relevant files are large, please provide a link to a public repository or a [Gist](https://gist.github.com/).
-->            

Changed files:
src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildTaskRunner.kt, src/main/kotlin/org/rust/cargo/runconfig/Utils.kt

diff --git a/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt b/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt
index 78516e898..21774d542 100644
--- a/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt
+++ b/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt
@@ -25,6 +25,7 @@ import org.rust.cargo.runconfig.filters.RsExplainFilter
 import org.rust.cargo.runconfig.filters.RsPanicFilter
 import org.rust.cargo.toolchain.CargoCommandLine
 import org.rust.cargo.toolchain.run
+import org.rust.openapiext.checkIsDispatchThread
 import org.rust.stdext.buildList
 
 fun CargoCommandLine.mergeWithDefault(default: CargoCommandConfiguration): CargoCommandLine =
@@ -48,6 +49,7 @@ fun RunManager.createCargoCommandRunConfiguration(cargoCommandLine: CargoCommand
 val Project.hasCargoProject: Boolean get() = cargoProjects.allProjects.isNotEmpty()
 
 fun Project.buildProject() {
+    checkIsDispatchThread()
     val arguments = buildList<String> {
         val settings = rustSettings
         add("--all")
@@ -61,9 +63,7 @@ fun Project.buildProject() {
     }
 
     // Initialize run content manager
-    invokeAndWaitIfNeeded {
-        RunContentManager.getInstance(this)
-    }
+    RunContentManager.getInstance(this)
 
     for (cargoProject in cargoProjects.allProjects) {
         CargoCommandLine.forProject(cargoProject, "build", arguments).run(cargoProject, saveConfiguration = false)
diff --git a/src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildTaskRunner.kt b/src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildTaskRunner.kt
index 8d4d9fcf1..5b6335629 100644
--- a/src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildTaskRunner.kt
+++ b/src/main/kotlin/org/rust/cargo/runconfig/buildtool/CargoBuildTaskRunner.kt
@@ -14,6 +14,7 @@ import com.intellij.execution.RunManager
 import com.intellij.execution.executors.DefaultRunExecutor
 import com.intellij.execution.runners.ExecutionEnvironment
 import com.intellij.execution.runners.ProgramRunner
+import com.intellij.openapi.application.invokeLater
 import com.intellij.openapi.diagnostic.Logger
 import com.intellij.openapi.progress.*
 import com.intellij.openapi.project.Project
@@ -49,7 +50,7 @@ class CargoBuildTaskRunner : ProjectTaskRunner() {
         if (project.isDisposed) return
 
         if (!project.isBuildToolWindowEnabled) {
-            project.buildProject()
+            invokeLater { project.buildProject() }
             return
         }