diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs index b7b17e4..825708e 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectCreditsTask.cs @@ -77,29 +77,23 @@ public class DetectCreditsTask : IScheduledTask Entrypoint.CancelAutomaticTask(cancellationToken); } - ScheduledTaskSemaphore.Wait(-1, cancellationToken); - - if (cancellationToken.IsCancellationRequested) + using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken)) { - ScheduledTaskSemaphore.Release(); + _logger.LogInformation("Scheduled Task is starting"); + + Plugin.Instance!.Configuration.PathRestrictions.Clear(); + var modes = new List { AnalysisMode.Credits }; + + var baseCreditAnalyzer = new BaseItemAnalyzerTask( + modes.AsReadOnly(), + _loggerFactory.CreateLogger(), + _loggerFactory, + _libraryManager); + + baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken); + return Task.CompletedTask; } - - _logger.LogInformation("Scheduled Task is starting"); - - Plugin.Instance!.Configuration.PathRestrictions.Clear(); - var modes = new List { AnalysisMode.Credits }; - - var baseCreditAnalyzer = new BaseItemAnalyzerTask( - modes.AsReadOnly(), - _loggerFactory.CreateLogger(), - _loggerFactory, - _libraryManager); - - baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken); - - ScheduledTaskSemaphore.Release(); - return Task.CompletedTask; } /// diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosCreditsTask.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosCreditsTask.cs index aea91ab..126a232 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosCreditsTask.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosCreditsTask.cs @@ -76,29 +76,23 @@ public class DetectIntrosCreditsTask : IScheduledTask Entrypoint.CancelAutomaticTask(cancellationToken); } - ScheduledTaskSemaphore.Wait(-1, cancellationToken); - - if (cancellationToken.IsCancellationRequested) + using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken)) { - ScheduledTaskSemaphore.Release(); + _logger.LogInformation("Scheduled Task is starting"); + + Plugin.Instance!.Configuration.PathRestrictions.Clear(); + var modes = new List { AnalysisMode.Introduction, AnalysisMode.Credits }; + + var baseIntroAnalyzer = new BaseItemAnalyzerTask( + modes.AsReadOnly(), + _loggerFactory.CreateLogger(), + _loggerFactory, + _libraryManager); + + baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken); + return Task.CompletedTask; } - - _logger.LogInformation("Scheduled Task is starting"); - - Plugin.Instance!.Configuration.PathRestrictions.Clear(); - var modes = new List { AnalysisMode.Introduction, AnalysisMode.Credits }; - - var baseIntroAnalyzer = new BaseItemAnalyzerTask( - modes.AsReadOnly(), - _loggerFactory.CreateLogger(), - _loggerFactory, - _libraryManager); - - baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken); - - ScheduledTaskSemaphore.Release(); - return Task.CompletedTask; } /// diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosTask.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosTask.cs index 1ba8900..0f7cfbf 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosTask.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/DetectIntrosTask.cs @@ -76,29 +76,23 @@ public class DetectIntrosTask : IScheduledTask Entrypoint.CancelAutomaticTask(cancellationToken); } - ScheduledTaskSemaphore.Wait(-1, cancellationToken); - - if (cancellationToken.IsCancellationRequested) + using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken)) { - ScheduledTaskSemaphore.Release(); + _logger.LogInformation("Scheduled Task is starting"); + + Plugin.Instance!.Configuration.PathRestrictions.Clear(); + var modes = new List { AnalysisMode.Introduction }; + + var baseIntroAnalyzer = new BaseItemAnalyzerTask( + modes.AsReadOnly(), + _loggerFactory.CreateLogger(), + _loggerFactory, + _libraryManager); + + baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken); + return Task.CompletedTask; } - - _logger.LogInformation("Scheduled Task is starting"); - - Plugin.Instance!.Configuration.PathRestrictions.Clear(); - var modes = new List { AnalysisMode.Introduction }; - - var baseIntroAnalyzer = new BaseItemAnalyzerTask( - modes.AsReadOnly(), - _loggerFactory.CreateLogger(), - _loggerFactory, - _libraryManager); - - baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken); - - ScheduledTaskSemaphore.Release(); - return Task.CompletedTask; } /// diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/ScheduledTaskSemaphore.cs b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/ScheduledTaskSemaphore.cs index 67b41a2..8b6c2b3 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/ScheduledTaskSemaphore.cs +++ b/ConfusedPolarBear.Plugin.IntroSkipper/ScheduledTasks/ScheduledTaskSemaphore.cs @@ -7,20 +7,18 @@ internal sealed class ScheduledTaskSemaphore : IDisposable { private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); + private static bool _isHeld = false; + private ScheduledTaskSemaphore() { } public static int CurrentCount => _semaphore.CurrentCount; - public static bool Wait(int timeout, CancellationToken cancellationToken) + public static IDisposable Acquire(int timeout, CancellationToken cancellationToken) { - return _semaphore.Wait(timeout, cancellationToken); - } - - public static int Release() - { - return _semaphore.Release(); + _isHeld = _semaphore.Wait(timeout, cancellationToken); + return new ScheduledTaskSemaphore(); } /// @@ -28,21 +26,10 @@ internal sealed class ScheduledTaskSemaphore : IDisposable /// public void Dispose() { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Protected dispose. - /// - /// Dispose. - private void Dispose(bool disposing) - { - if (!disposing) + if (_isHeld) // Release only if acquired { - return; + _semaphore.Release(); + _isHeld = false; } - - _semaphore.Dispose(); } }