diff --git a/src/StructuredLogViewer.Core/PreprocessedFileManager.cs b/src/StructuredLogViewer.Core/PreprocessedFileManager.cs index 505cbb94..cbd07a85 100644 --- a/src/StructuredLogViewer.Core/PreprocessedFileManager.cs +++ b/src/StructuredLogViewer.Core/PreprocessedFileManager.cs @@ -49,7 +49,7 @@ public void BuildImportMap() string evaluationKey = GetEvaluationKey(projectEvaluation); var importMap = GetOrCreateImportMap(evaluationKey, projectEvaluation.ProjectFile); - System.Threading.Tasks.Task.Run(() => + build.RunInBackground(() => { imports.VisitAllChildren(import => VisitImport(import, importMap)); }); diff --git a/src/StructuredLogger/BinaryLog.cs b/src/StructuredLogger/BinaryLog.cs index e8f6af37..890d1a1f 100644 --- a/src/StructuredLogger/BinaryLog.cs +++ b/src/StructuredLogger/BinaryLog.cs @@ -131,6 +131,8 @@ public static Build ReadBuild(Stream stream, Progress progress, byte[] projectIm // Serialization.WriteStringsToFile(@"C:\temp\1.txt", strings.ToArray()); + build.WaitForBackgroundTasks(); + return build; } } diff --git a/src/StructuredLogger/Construction/Construction.cs b/src/StructuredLogger/Construction/Construction.cs index 57b64128..d2085cbb 100644 --- a/src/StructuredLogger/Construction/Construction.cs +++ b/src/StructuredLogger/Construction/Construction.cs @@ -28,8 +28,6 @@ public class Construction private readonly MessageProcessor messageProcessor; private readonly StringCache stringTable; - internal bool PopulatePropertiesAndItemsInBackground = PlatformUtilities.HasThreads; - public StringCache StringTable => stringTable; public NamedNode EvaluationFolder => Build.EvaluationFolder; @@ -566,14 +564,7 @@ public void StatusEventRaised(object sender, BuildStatusEventArgs e) propertiesFolder.DisableChildrenCache = true; } - if (PopulatePropertiesAndItemsInBackground) - { - System.Threading.Tasks.Task.Run(() => AddGlobalProperties()); - } - else - { - AddGlobalProperties(); - } + Build.RunInBackground(() => AddGlobalProperties()); void AddGlobalProperties() { @@ -988,14 +979,7 @@ public void UpdateProject(Project project, ProjectStartedEventArgs args) propertyFolder.DisableChildrenCache = true; } - if (PopulatePropertiesAndItemsInBackground) - { - System.Threading.Tasks.Task.Run(() => AddGlobalProperties()); - } - else - { - AddGlobalProperties(); - } + Build.RunInBackground(() => AddGlobalProperties()); void AddGlobalProperties() { diff --git a/src/StructuredLogger/Construction/MessageProcessor.cs b/src/StructuredLogger/Construction/MessageProcessor.cs index bb9052ec..092d75a3 100644 --- a/src/StructuredLogger/Construction/MessageProcessor.cs +++ b/src/StructuredLogger/Construction/MessageProcessor.cs @@ -275,14 +275,7 @@ private BaseNode CreateParameterNode(string itemName, IEnumerable items, bool is private void AddItems(IEnumerable items, TreeNode parent) { - if (construction.PopulatePropertiesAndItemsInBackground) - { - System.Threading.Tasks.Task.Run(() => AddItemsCore(items, parent)); - } - else - { - AddItemsCore(items, parent); - } + construction.Build.RunInBackground(() => AddItemsCore(items, parent)); } private void AddItemsCore(IEnumerable items, TreeNode parent) diff --git a/src/StructuredLogger/ObjectModel/Build.cs b/src/StructuredLogger/ObjectModel/Build.cs index 00351f70..efdf2dfe 100644 --- a/src/StructuredLogger/ObjectModel/Build.cs +++ b/src/StructuredLogger/ObjectModel/Build.cs @@ -2,8 +2,8 @@ using System.Collections.Generic; using System.IO; using System.IO.Compression; -using System.Linq; using System.Threading; +using TPLTask = System.Threading.Tasks.Task; namespace Microsoft.Build.Logging.StructuredLogger { @@ -23,6 +23,8 @@ public class Build : TimedNode public int FileFormatVersion { get; set; } public byte[] SourceFilesArchive { get; set; } + private readonly List backgroundTasks = new List(); + private IReadOnlyList sourceFiles; public IReadOnlyList SourceFiles { @@ -39,7 +41,7 @@ public IReadOnlyList SourceFiles } private string msbuildVersion; - public string MSBuildVersion + public string MSBuildVersion { get => msbuildVersion; set @@ -255,5 +257,34 @@ public ProjectEvaluation FindEvaluation(int id) return projectEvaluation; } + + public void RunInBackground(Action action) + { + if (PlatformUtilities.HasThreads) + { + var task = TPLTask.Run(action); + lock (backgroundTasks) + { + backgroundTasks.Add(task); + } + } + else + { + action(); + } + } + + public void WaitForBackgroundTasks() + { + lock (backgroundTasks) + { + foreach (var task in backgroundTasks) + { + task.Wait(); + } + + backgroundTasks.Clear(); + } + } } }