Refactor ScheduledTaskSemaphore (#148)
This commit is contained in:
parent
482f573ecb
commit
605c71f4aa
@ -77,14 +77,8 @@ public class DetectCreditsTask : IScheduledTask
|
||||
Entrypoint.CancelAutomaticTask(cancellationToken);
|
||||
}
|
||||
|
||||
ScheduledTaskSemaphore.Wait(-1, cancellationToken);
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken))
|
||||
{
|
||||
ScheduledTaskSemaphore.Release();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Scheduled Task is starting");
|
||||
|
||||
Plugin.Instance!.Configuration.PathRestrictions.Clear();
|
||||
@ -98,9 +92,9 @@ public class DetectCreditsTask : IScheduledTask
|
||||
|
||||
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
|
||||
ScheduledTaskSemaphore.Release();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get task triggers.
|
||||
|
@ -76,14 +76,8 @@ public class DetectIntrosCreditsTask : IScheduledTask
|
||||
Entrypoint.CancelAutomaticTask(cancellationToken);
|
||||
}
|
||||
|
||||
ScheduledTaskSemaphore.Wait(-1, cancellationToken);
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken))
|
||||
{
|
||||
ScheduledTaskSemaphore.Release();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Scheduled Task is starting");
|
||||
|
||||
Plugin.Instance!.Configuration.PathRestrictions.Clear();
|
||||
@ -97,9 +91,9 @@ public class DetectIntrosCreditsTask : IScheduledTask
|
||||
|
||||
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
|
||||
ScheduledTaskSemaphore.Release();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get task triggers.
|
||||
|
@ -76,14 +76,8 @@ public class DetectIntrosTask : IScheduledTask
|
||||
Entrypoint.CancelAutomaticTask(cancellationToken);
|
||||
}
|
||||
|
||||
ScheduledTaskSemaphore.Wait(-1, cancellationToken);
|
||||
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken))
|
||||
{
|
||||
ScheduledTaskSemaphore.Release();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Scheduled Task is starting");
|
||||
|
||||
Plugin.Instance!.Configuration.PathRestrictions.Clear();
|
||||
@ -97,9 +91,9 @@ public class DetectIntrosTask : IScheduledTask
|
||||
|
||||
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
|
||||
ScheduledTaskSemaphore.Release();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get task triggers.
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -28,21 +26,10 @@ internal sealed class ScheduledTaskSemaphore : IDisposable
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Protected dispose.
|
||||
/// </summary>
|
||||
/// <param name="disposing">Dispose.</param>
|
||||
private void Dispose(bool disposing)
|
||||
if (_isHeld) // Release only if acquired
|
||||
{
|
||||
if (!disposing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_semaphore.Dispose();
|
||||
_semaphore.Release();
|
||||
_isHeld = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user