| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System;
- using System.Threading.Tasks;
- using Quadarax.Foundation.Core.Logging;
- namespace Quadarax.Foundation.Core.Thread
- {
- public class LoopWorker : AbstractWorker
- {
- #region *** Private fields ***
- private TimeSpan _defaultTimeout;
- private long _cntLimit;
- private Func<IWorkerContext, Task> _customIteration;
- #endregion
- #region *** Properties ***
- public long IterationsLimit { get; set; } = 0;
- public TimeSpan IterationsDelayBetween { get; set; } = TimeSpan.Zero;
- #endregion
- #region *** Constructors ***
- public LoopWorker(IWorkerContext context) : base(context)
- {
- }
- public LoopWorker(string workerName, IWorkerContext context) : base(workerName, context)
- {
- }
- public LoopWorker(string workerName) : base(workerName)
- {
- }
- public LoopWorker(string workerName, IWorkerContext context, ILogger logger = null) : base(workerName, context, logger)
- {
- }
- public LoopWorker(string workerName, IWorkerContext context,Func<IWorkerContext, Task> customIteration = null, ILogger logger = null) : base(workerName, context, logger)
- {
- _customIteration = customIteration;
- }
- #endregion
- #region *** Public Operations ***
- #endregion
- #region *** Virtuals and protected ***
- protected override async Task Do(IWorkerContext context)
- {
- var exit = false;
- var first = true;
- while (!exit)
- {
- if (first)
- first = false;
- else if (IterationsDelayBetween != TimeSpan.Zero)
- System.Threading.Thread.CurrentThread.Join(IterationsDelayBetween);
- if (IterationsLimit == 0)
- {
- if (_customIteration != null)
- await _customIteration(context);
- else
- await DoIteration(context);
- }
- else
- {
- if (_customIteration != null)
- await _customIteration(context);
- else
- await DoIteration(context);
- _cntLimit++;
- if (_cntLimit > IterationsLimit)
- exit = true;
- }
- }
- }
- #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
- protected virtual async Task DoIteration(IWorkerContext context)
- #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
- {
- }
- protected override void OnBeforeStart()
- {
- base.OnBeforeStart();
- _defaultTimeout = Timeout;
- Timeout = TimeSpan.Zero;
- }
- protected override void OnAfterStop()
- {
- base.OnAfterStop();
- Timeout = _defaultTimeout;
- _cntLimit = 0;
- }
- #endregion
- #region *** Private Operations ***
- #endregion
- }
- }
|