Skip to content

Commit 7455620

Browse files
author
Paul van Brenk
committed
Add the output panes only once to the collection
1 parent 5914237 commit 7455620

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Nodejs/Product/Nodejs/SharedProject/OutputWindowWrapper.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.Concurrent;
45
using System.Collections.Generic;
56
using System.ComponentModel.Composition;
67
using System.Diagnostics;
@@ -22,7 +23,7 @@ public sealed class OutputPaneWrapper
2223
[Import]
2324
private SVsServiceProvider ServiceProvider { get; set; }
2425

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

2728
private IVsOutputWindowPane InitializeOutputPane(string title, Guid paneId)
2829
{
@@ -51,23 +52,32 @@ private IVsOutputWindowPane InitializeOutputPane(string title, Guid paneId)
5152

5253
public void WriteLine(string message, OutputWindowTarget target = OutputWindowTarget.Npm)
5354
{
54-
if (!this.lazyOutputPaneCollection.TryGetValue(target, out var lazyOutputPane) || lazyOutputPane == null)
55+
if (!this.IsInitialized())
5556
{
5657
throw new InvalidOperationException("You need to initialize the output panes before using them.");
5758
}
5859

59-
var hr = lazyOutputPane.OutputStringThreadSafe(message + Environment.NewLine);
60+
var hr = this.lazyOutputPaneCollection[target].OutputStringThreadSafe(message + Environment.NewLine);
6061
Debug.Assert(ErrorHandler.Succeeded(hr));
6162
}
6263

6364
public void InitializeOutputPanes()
6465
{
6566
ThreadHelper.ThrowIfNotOnUIThread();
6667

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);
68+
if (!this.IsInitialized())
69+
{
70+
var npmPane = InitializeOutputPane("Npm", VSPackageManagerPaneGuid);
71+
this.lazyOutputPaneCollection.TryAdd(OutputWindowTarget.Npm, npmPane);
72+
var tscPane = InitializeOutputPane("Tsc", TscPaneGuid);
73+
this.lazyOutputPaneCollection.TryAdd(OutputWindowTarget.Tsc, tscPane);
74+
}
75+
}
76+
77+
private bool IsInitialized()
78+
{
79+
return this.lazyOutputPaneCollection.TryGetValue(OutputWindowTarget.Npm, out var npmWindow) && npmWindow != null
80+
&& this.lazyOutputPaneCollection.TryGetValue(OutputWindowTarget.Tsc, out var tscWindow) && tscWindow != null;
7181
}
7282
}
7383

0 commit comments

Comments
 (0)