Correctly Dispose Entrypoint Resources

This commit is contained in:
rlauu 2024-06-28 17:06:41 +02:00
parent b9b9e88765
commit 7fa73a1d3d
2 changed files with 17 additions and 16 deletions

View File

@ -89,17 +89,17 @@ public class ChapterAnalyzer : IMediaFileAnalyzer
string expression, string expression,
AnalysisMode mode) AnalysisMode mode)
{ {
if (chapters.Count == 0) var count = chapters.Count;
if (count == 0)
{ {
return null; return null;
} }
var config = Plugin.Instance?.Configuration ?? new PluginConfiguration(); var config = Plugin.Instance?.Configuration ?? new PluginConfiguration();
var count = chapters.Count;
var reversed = mode != AnalysisMode.Introduction; var reversed = mode != AnalysisMode.Introduction;
var (minDuration, maxDuration) = !reversed var (minDuration, maxDuration) = reversed
? (config.MinimumIntroDuration, config.MaximumIntroDuration) ? (config.MinimumCreditsDuration, config.MaximumCreditsDuration)
: (config.MinimumCreditsDuration, config.MaximumCreditsDuration); : (config.MinimumIntroDuration, config.MaximumIntroDuration);
// Check all chapters // Check all chapters
for (int i = reversed ? count - 1 : 0; reversed ? i >= 0 : i < count; i += reversed ? -1 : 1) for (int i = reversed ? count - 1 : 0; reversed ? i >= 0 : i < count; i += reversed ? -1 : 1)

View File

@ -24,6 +24,7 @@ public class Entrypoint : IHostedService, IDisposable
private readonly ILogger<Entrypoint> _logger; private readonly ILogger<Entrypoint> _logger;
private readonly ILoggerFactory _loggerFactory; private readonly ILoggerFactory _loggerFactory;
private Timer _queueTimer; private Timer _queueTimer;
private bool _disposed;
private bool _analyzeAgain; private bool _analyzeAgain;
private HashSet<Guid> _seasonsToAnalyze = new HashSet<Guid>(); private HashSet<Guid> _seasonsToAnalyze = new HashSet<Guid>();
private static CancellationTokenSource? _cancellationTokenSource; private static CancellationTokenSource? _cancellationTokenSource;
@ -111,13 +112,6 @@ public class Entrypoint : IHostedService, IDisposable
// Stop the timer // Stop the timer
_queueTimer.Change(Timeout.Infinite, 0); _queueTimer.Change(Timeout.Infinite, 0);
if (_cancellationTokenSource != null) // Null Check
{
_cancellationTokenSource.Dispose();
_cancellationTokenSource = null;
}
return Task.CompletedTask; return Task.CompletedTask;
} }
@ -338,12 +332,19 @@ public class Entrypoint : IHostedService, IDisposable
/// <summary> /// <summary>
/// Protected dispose. /// Protected dispose.
/// </summary> /// </summary>
/// <param name="dispose">Dispose.</param> /// <param name="disposing">Dispose.</param>
protected virtual void Dispose(bool dispose) protected virtual void Dispose(bool disposing)
{ {
if (!dispose) if (!_disposed)
{ {
_queueTimer.Dispose(); if (disposing)
{
_queueTimer.Dispose();
_cancellationTokenSource?.Dispose();
_autoTaskCompletEvent.Dispose();
}
_disposed = true;
} }
} }
} }