Browse Source

BackgroundWorkers should be restartable to permit changes in Timer interval

As BackgroundWorkers are a Singleton, they should be restartable to permit changes in Timer interval.
pull/19986/head
machonky 2 years ago
parent
commit
96c562b8bb
  1. 13
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs

13
framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs

@ -22,10 +22,16 @@ public abstract class BackgroundWorkerBase : IBackgroundWorker
protected ILogger Logger => LazyServiceProvider.LazyGetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName!) ?? NullLogger.Instance);
protected CancellationTokenSource StoppingTokenSource { get; }
protected CancellationToken StoppingToken { get; }
protected CancellationTokenSource StoppingTokenSource { get; private set; }
protected CancellationToken StoppingToken { get; private set; }
public BackgroundWorkerBase()
{
ResetStoppingCancellationTokenSource();
}
private void ResetStoppingCancellationTokenSource()
{
StoppingTokenSource = new CancellationTokenSource();
StoppingToken = StoppingTokenSource.Token;
@ -42,6 +48,9 @@ public abstract class BackgroundWorkerBase : IBackgroundWorker
Logger.LogDebug("Stopped background worker: " + ToString());
StoppingTokenSource.Cancel();
StoppingTokenSource.Dispose();
ResetStoppingCancellationTokenSource();
return Task.CompletedTask;
}

Loading…
Cancel
Save