Skip to content

Commit cd4afe1

Browse files
authored
Merge pull request #1932 from paulvanbrenk/outputwindow
Add the output panes only once to the collection
2 parents fb0ea79 + ea45bb7 commit cd4afe1

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

Nodejs/Product/Nodejs/SharedProject/OutputWindowWrapper.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

33
using System;
4-
using System.Collections.Generic;
4+
using System.Collections.Concurrent;
55
using System.ComponentModel.Composition;
66
using System.Diagnostics;
77
using Microsoft.VisualStudio;
@@ -22,7 +22,7 @@ public sealed class OutputPaneWrapper
2222
[Import]
2323
private SVsServiceProvider ServiceProvider { get; set; }
2424

25-
private Dictionary<OutputWindowTarget, IVsOutputWindowPane> lazyOutputPaneCollection = new Dictionary<OutputWindowTarget, IVsOutputWindowPane>();
25+
private ConcurrentDictionary<OutputWindowTarget, IVsOutputWindowPane> lazyOutputPaneCollection = new ConcurrentDictionary<OutputWindowTarget, IVsOutputWindowPane>();
2626

2727
private IVsOutputWindowPane InitializeOutputPane(string title, Guid paneId)
2828
{
@@ -51,23 +51,32 @@ private IVsOutputWindowPane InitializeOutputPane(string title, Guid paneId)
5151

5252
public void WriteLine(string message, OutputWindowTarget target = OutputWindowTarget.Npm)
5353
{
54-
if (!this.lazyOutputPaneCollection.TryGetValue(target, out var lazyOutputPane) || lazyOutputPane == null)
54+
if (!this.IsInitialized())
5555
{
5656
throw new InvalidOperationException("You need to initialize the output panes before using them.");
5757
}
5858

59-
var hr = lazyOutputPane.OutputStringThreadSafe(message + Environment.NewLine);
59+
var hr = this.lazyOutputPaneCollection[target].OutputStringThreadSafe(message + Environment.NewLine);
6060
Debug.Assert(ErrorHandler.Succeeded(hr));
6161
}
6262

6363
public void InitializeOutputPanes()
6464
{
6565
ThreadHelper.ThrowIfNotOnUIThread();
6666

67-
var npmPane = InitializeOutputPane("Npm", VSPackageManagerPaneGuid);
68-
this.lazyOutputPaneCollection.Add(OutputWindowTarget.Npm, npmPane);
69-
var tscPane = InitializeOutputPane("Tsc", TscPaneGuid);
70-
this.lazyOutputPaneCollection.Add(OutputWindowTarget.Tsc, tscPane);
67+
if (!this.IsInitialized())
68+
{
69+
var npmPane = InitializeOutputPane("Npm", VSPackageManagerPaneGuid);
70+
this.lazyOutputPaneCollection.TryAdd(OutputWindowTarget.Npm, npmPane);
71+
var tscPane = InitializeOutputPane("Tsc", TscPaneGuid);
72+
this.lazyOutputPaneCollection.TryAdd(OutputWindowTarget.Tsc, tscPane);
73+
}
74+
}
75+
76+
private bool IsInitialized()
77+
{
78+
return this.lazyOutputPaneCollection.TryGetValue(OutputWindowTarget.Npm, out var npmWindow) && npmWindow != null
79+
&& this.lazyOutputPaneCollection.TryGetValue(OutputWindowTarget.Tsc, out var tscWindow) && tscWindow != null;
7180
}
7281
}
7382

0 commit comments

Comments
 (0)