diff --git a/CodeLineCounter.Tests/TestBase.cs b/CodeLineCounter.Tests/TestBase.cs index 018f3b6..35d6df3 100644 --- a/CodeLineCounter.Tests/TestBase.cs +++ b/CodeLineCounter.Tests/TestBase.cs @@ -7,7 +7,7 @@ namespace CodeLineCounter.Tests { public abstract class TestBase : IDisposable { - private static readonly object _consoleLock = new object(); + private static readonly Lock _consoleLock = new (); private readonly ThreadLocal _stringWriter = new ThreadLocal(() => new StringWriter()); private readonly ThreadLocal _stringReader = new ThreadLocal(() => new StringReader(string.Empty)); private readonly ThreadLocal _originalConsoleOut = new ThreadLocal(); @@ -22,7 +22,7 @@ protected TestBase() protected void initialization() { - lock (_consoleLock) + using (_consoleLock.EnterScope()) { _originalConsoleOut.Value = Console.Out; _originalConsoleIn.Value = Console.In; @@ -32,7 +32,7 @@ protected void initialization() protected void RedirectConsoleInputOutput() { - lock (_consoleLock) + using (_consoleLock.EnterScope()) { if (_stringWriter.Value != null) { @@ -47,7 +47,7 @@ protected void RedirectConsoleInputOutput() protected string GetConsoleOutput() { - lock (_consoleLock) + using (_consoleLock.EnterScope()) { if (_stringWriter.Value != null) { @@ -57,17 +57,10 @@ protected string GetConsoleOutput() } } - protected void SetConsoleInput(string input) - { - lock (_consoleLock) - { - _stringReader.Value = new StringReader(input); - } - } protected void ResetConsoleInputOutput() { - lock (_consoleLock) + using (_consoleLock.EnterScope()) { if (_originalConsoleOut != null && _originalConsoleOut.Value != null) { diff --git a/CodeLineCounter/Services/CodeDuplicationChecker.cs b/CodeLineCounter/Services/CodeDuplicationChecker.cs index 7d3d7ca..bda7817 100644 --- a/CodeLineCounter/Services/CodeDuplicationChecker.cs +++ b/CodeLineCounter/Services/CodeDuplicationChecker.cs @@ -11,7 +11,7 @@ public class CodeDuplicationChecker { private readonly ConcurrentDictionary> duplicationMap; private readonly ConcurrentDictionary> hashMap; - private readonly object duplicationLock = new(); + private readonly Lock duplicationLock = new(); public CodeDuplicationChecker() { @@ -81,7 +81,7 @@ public void DetectCodeDuplicationInSourceCode(string normalizedPath, string sour public void UpdateDuplicationMap() { - lock (duplicationLock) + using (duplicationLock.EnterScope()) { // Clear previous results to avoid stale data. duplicationMap.Clear(); diff --git a/CodeLineCounter/Services/DependencyAnalyzer.cs b/CodeLineCounter/Services/DependencyAnalyzer.cs index 9470354..a80e6ea 100644 --- a/CodeLineCounter/Services/DependencyAnalyzer.cs +++ b/CodeLineCounter/Services/DependencyAnalyzer.cs @@ -12,7 +12,7 @@ public static class DependencyAnalyzer { private static readonly ConcurrentDictionary> _dependencyMap = new(); private static readonly HashSet _solutionClasses = new(); - private static readonly object _dependencyLock = new(); + private static readonly Lock _dependencyLock = new(); public static void AnalyzeSolution(string solutionFilePath) { @@ -39,7 +39,7 @@ private static void CollectAllClasses(IEnumerable projectFiles) .OfType() .Select(c => GetFullTypeName(c)); - lock (_dependencyLock) + using (_dependencyLock.EnterScope()) { foreach (var className in classes) {