From 14e28641b3d5d58450d835c0facea18b1b8b3e48 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 29 Aug 2016 11:51:17 -0700 Subject: [PATCH 1/2] Fix Building NTVS with devenv /Build Issue #1230 **Bug** Crash when trying to build an NTVS project from the command line with `devenv /build`. The root cause is in the `UIThread` where we attempt to call `TaskScheduler.FromCurrentSynchronizationContext`. In non-ui flows, this throws an `InvalidOperationException`. **Fix** If `TaskScheduler.FromCurrentSynchronizationContext` fails, try setting the current syntronization context manually and retrying the call. This fixes the `UIThread`, but also fixes all other code paths (including some in the VS extension bases themseleves) that use `TaskScheduler.FromCurrentSynchronizationContext` or assume a SyncronizationContext exists. closes #1230 --- Common/Product/SharedProject/UIThread.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Common/Product/SharedProject/UIThread.cs b/Common/Product/SharedProject/UIThread.cs index f94413da1..3c6421d08 100644 --- a/Common/Product/SharedProject/UIThread.cs +++ b/Common/Product/SharedProject/UIThread.cs @@ -26,7 +26,12 @@ class UIThread : UIThreadBase { private readonly Thread _uiThread; private UIThread() { - _scheduler = TaskScheduler.FromCurrentSynchronizationContext(); + try { + _scheduler = TaskScheduler.FromCurrentSynchronizationContext(); + } catch (InvalidOperationException) { + SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); + _scheduler = TaskScheduler.FromCurrentSynchronizationContext(); + } _factory = new TaskFactory(_scheduler); _uiThread = Thread.CurrentThread; } From 682c519a7b47cc772f92c28593c5195f297b3ec7 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 29 Aug 2016 12:01:02 -0700 Subject: [PATCH 2/2] Better scope fix --- Common/Product/SharedProject/UIThread.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Common/Product/SharedProject/UIThread.cs b/Common/Product/SharedProject/UIThread.cs index 3c6421d08..8a3f6f8d7 100644 --- a/Common/Product/SharedProject/UIThread.cs +++ b/Common/Product/SharedProject/UIThread.cs @@ -26,12 +26,10 @@ class UIThread : UIThreadBase { private readonly Thread _uiThread; private UIThread() { - try { - _scheduler = TaskScheduler.FromCurrentSynchronizationContext(); - } catch (InvalidOperationException) { + if (SynchronizationContext.Current == null) { SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); - _scheduler = TaskScheduler.FromCurrentSynchronizationContext(); } + _scheduler = TaskScheduler.FromCurrentSynchronizationContext(); _factory = new TaskFactory(_scheduler); _uiThread = Thread.CurrentThread; }