diff --git a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/NonAnsiTerminal.cs b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/NonAnsiTerminal.cs index e0af85e4fc4..8c1f5457201 100644 --- a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/NonAnsiTerminal.cs +++ b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/NonAnsiTerminal.cs @@ -5,6 +5,7 @@ using System.Text; using Microsoft.Testing.Platform.Helpers; +using Microsoft.Testing.Platform.Resources; namespace Microsoft.Testing.Platform.OutputDevice.Terminal; @@ -122,7 +123,7 @@ public void StartUpdate() { if (_isBatching) { - throw new InvalidOperationException("Console is already in batching mode."); + throw new InvalidOperationException(PlatformResources.ConsoleIsAlreadyInBatchingMode); } _stringBuilder.Clear(); diff --git a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TestProgressStateAwareTerminal.cs b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TestProgressStateAwareTerminal.cs index f1b1f32c62f..c462d8e67e8 100644 --- a/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TestProgressStateAwareTerminal.cs +++ b/src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/TestProgressStateAwareTerminal.cs @@ -16,13 +16,17 @@ internal sealed partial class TestProgressStateAwareTerminal : IDisposable /// /// Protects access to state shared between the logger callbacks and the rendering thread. /// +#if NET9_0_OR_GREATER + private readonly System.Threading.Lock _lock = new(); +#else private readonly object _lock = new(); +#endif private readonly ITerminal _terminal; private readonly Func _showProgress; private readonly bool _writeProgressImmediatelyAfterOutput; private readonly int _updateEvery; - private TestProgressState?[] _progressItems = Array.Empty(); + private TestProgressState?[] _progressItems = []; private bool? _showProgressCached; /// @@ -122,24 +126,35 @@ internal void WriteToTerminal(Action write) { lock (_lock) { - _terminal.StartUpdate(); - _terminal.EraseProgress(); - write(_terminal); - if (_writeProgressImmediatelyAfterOutput) + try { - _terminal.RenderProgress(_progressItems); + _terminal.StartUpdate(); + _terminal.EraseProgress(); + write(_terminal); + if (_writeProgressImmediatelyAfterOutput) + { + _terminal.RenderProgress(_progressItems); + } + } + finally + { + _terminal.StopUpdate(); } - - _terminal.StopUpdate(); } } else { lock (_lock) { - _terminal.StartUpdate(); - write(_terminal); - _terminal.StopUpdate(); + try + { + _terminal.StartUpdate(); + write(_terminal); + } + finally + { + _terminal.StopUpdate(); + } } } }