From dd1c70152c026b5962bd04b8cc295eb798dcf2b2 Mon Sep 17 00:00:00 2001 From: MrLuje Date: Wed, 4 Sep 2019 13:32:43 +0200 Subject: [PATCH 1/4] prevent NRE on JoinableTaskFactory --- vs-commitizen.vs2015/VsCommitizenNavigationItem.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs b/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs index eb46d43..b0d70c6 100644 --- a/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs +++ b/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs @@ -37,9 +37,12 @@ private async void GitService_PropertyChanged(object sender, System.ComponentMod private async System.Threading.Tasks.Task UpdateIsVisibleAsync() { - await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); - this.IsVisible = this.gitService?.ActiveRepositories.Count > 0; - await TaskScheduler.Default; + if (VsTaskLibraryHelper.ServiceInstance != null) + { + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + this.IsVisible = this.gitService?.ActiveRepositories.Count > 0; + await TaskScheduler.Default; + } } public override void Execute() From fc331eb98c3ba5692c9c9c641c3e6065622b3701 Mon Sep 17 00:00:00 2001 From: MrLuje Date: Wed, 4 Sep 2019 14:13:23 +0200 Subject: [PATCH 2/4] more checks --- .../VsCommitizenNavigationItem.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs b/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs index b0d70c6..3d444f0 100644 --- a/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs +++ b/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs @@ -37,12 +37,16 @@ private async void GitService_PropertyChanged(object sender, System.ComponentMod private async System.Threading.Tasks.Task UpdateIsVisibleAsync() { - if (VsTaskLibraryHelper.ServiceInstance != null) - { - await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); - this.IsVisible = this.gitService?.ActiveRepositories.Count > 0; - await TaskScheduler.Default; - } + if (VsTaskLibraryHelper.ServiceInstance == null) + return; + + var joinableTaskFactory = ThreadHelper.JoinableTaskFactory; + if (joinableTaskFactory == null) + return; + + await joinableTaskFactory.SwitchToMainThreadAsync(); + this.IsVisible = this.gitService?.ActiveRepositories.Count > 0; + await TaskScheduler.Default; } public override void Execute() From fd8dbd2ba808b4d09a79b5450f237ecc65eaf263 Mon Sep 17 00:00:00 2001 From: MrLuje Date: Wed, 4 Sep 2019 16:24:02 +0200 Subject: [PATCH 3/4] prevent crash in UpdateIsVisibleAsync, write errors in output pane --- vs-commitizen.Settings/OutputPaneWriter.cs | 44 +++++++++++++++++++ .../vs-commitizen.Settings.csproj | 1 + .../VsCommitizenNavigationItem.cs | 14 ++++-- 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 vs-commitizen.Settings/OutputPaneWriter.cs diff --git a/vs-commitizen.Settings/OutputPaneWriter.cs b/vs-commitizen.Settings/OutputPaneWriter.cs new file mode 100644 index 0000000..bfecb1f --- /dev/null +++ b/vs-commitizen.Settings/OutputPaneWriter.cs @@ -0,0 +1,44 @@ +using System; +using Microsoft.VisualStudio; +using Microsoft.VisualStudio.Shell; +using Microsoft.VisualStudio.Shell.Interop; + +namespace vs_commitizen.Settings +{ + public static class OutputPaneWriter + { + private static Guid panelGuild = new Guid("5BB96421-E33D-40DA-9E8D-C657B7E94C70"); + + static IVsOutputWindowPane GetPane() + { + var outputWindow = Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow; + if (outputWindow == null) return null; + + IVsOutputWindowPane pane; + + if (ErrorHandler.Failed(outputWindow.GetPane(ref panelGuild, out pane)) && + (ErrorHandler.Succeeded(outputWindow.CreatePane(ref panelGuild, "vs-commitizen", 1, 1)))) + { + outputWindow.GetPane(ref panelGuild, out pane); + } + + ErrorHandler.ThrowOnFailure(pane.Activate()); + + return pane; + } + + public static void Print(string message) + { + var pane = GetPane(); + + ErrorHandler.ThrowOnFailure(pane.OutputString(message + Environment.NewLine)); + } + + public static void Print(string str, Exception ex) + { + var pane = GetPane(); + + ErrorHandler.ThrowOnFailure(pane.OutputString(str + "\r\n\r\n" + ex)); + } + } +} \ No newline at end of file diff --git a/vs-commitizen.Settings/vs-commitizen.Settings.csproj b/vs-commitizen.Settings/vs-commitizen.Settings.csproj index e7954ec..4a52bb9 100644 --- a/vs-commitizen.Settings/vs-commitizen.Settings.csproj +++ b/vs-commitizen.Settings/vs-commitizen.Settings.csproj @@ -45,6 +45,7 @@ + Component diff --git a/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs b/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs index 3d444f0..845cb52 100644 --- a/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs +++ b/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs @@ -6,6 +6,7 @@ using System.ComponentModel.Composition; using System.Drawing; using System.Threading.Tasks; +using vs_commitizen.Settings; using vs_commitizen.vs; namespace vs_commitizen.vs2015 @@ -44,9 +45,16 @@ private async System.Threading.Tasks.Task UpdateIsVisibleAsync() if (joinableTaskFactory == null) return; - await joinableTaskFactory.SwitchToMainThreadAsync(); - this.IsVisible = this.gitService?.ActiveRepositories.Count > 0; - await TaskScheduler.Default; + try + { + await joinableTaskFactory.SwitchToMainThreadAsync(); + this.IsVisible = this.gitService?.ActiveRepositories.Count > 0; + await TaskScheduler.Default; + } + catch (Exception ex) + { + OutputPaneWriter.Print($"UpdateIsVisibleAsync: {ex}"); + } } public override void Execute() From 0f631cf1cc059e42577ea07c54db73e406376d02 Mon Sep 17 00:00:00 2001 From: MrLuje Date: Thu, 5 Sep 2019 09:58:27 +0200 Subject: [PATCH 4/4] try catch JoinableTaskFactory --- vs-commitizen.vs2015/VsCommitizenNavigationItem.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs b/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs index 845cb52..ecc6c10 100644 --- a/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs +++ b/vs-commitizen.vs2015/VsCommitizenNavigationItem.cs @@ -41,12 +41,12 @@ private async System.Threading.Tasks.Task UpdateIsVisibleAsync() if (VsTaskLibraryHelper.ServiceInstance == null) return; - var joinableTaskFactory = ThreadHelper.JoinableTaskFactory; - if (joinableTaskFactory == null) - return; - try { + var joinableTaskFactory = ThreadHelper.JoinableTaskFactory; + if (joinableTaskFactory == null) + return; + await joinableTaskFactory.SwitchToMainThreadAsync(); this.IsVisible = this.gitService?.ActiveRepositories.Count > 0; await TaskScheduler.Default;