Browse Source

extends Thread/LoopWorker to support custom overlaping iterations

Dalibor Votruba 2 years ago
parent
commit
af5b66e3bc
2 changed files with 18 additions and 3 deletions
  1. 15 1
      qdr.fnd.core/Thread/LoopWorker.cs
  2. 3 2
      qdr.fnd.core/releasenotes.md

+ 15 - 1
qdr.fnd.core/Thread/LoopWorker.cs

@@ -10,11 +10,17 @@ namespace Quadarax.Foundation.Core.Thread
         private TimeSpan _defaultTimeout;
         private TimeSpan _defaultTimeout;
         private long _cntLimit;
         private long _cntLimit;
         private Func<IWorkerContext?, Task>? _customIteration;
         private Func<IWorkerContext?, Task>? _customIteration;
+        private bool _isIterationRunning = false;
         #endregion
         #endregion
 
 
         #region *** Properties ***
         #region *** Properties ***
         public long IterationsLimit { get; set; } = 0;
         public long IterationsLimit { get; set; } = 0;
         public TimeSpan IterationsDelayBetween { get; set; } = TimeSpan.Zero;
         public TimeSpan IterationsDelayBetween { get; set; } = TimeSpan.Zero;
+        /// <summary>
+        /// Defines if overlap iteration is allowed. If false, iteration is skipped if previous iteration is not finished.
+        /// Default value is false.
+        /// </summary>
+        public bool AllowOverlap { get; set; } = false;
         #endregion
         #endregion
 
 
         #region *** Constructors ***
         #region *** Constructors ***
@@ -52,8 +58,15 @@ namespace Quadarax.Foundation.Core.Thread
                 if (first)
                 if (first)
                     first = false;
                     first = false;
                 else if (IterationsDelayBetween != TimeSpan.Zero)
                 else if (IterationsDelayBetween != TimeSpan.Zero)
-                    System.Threading.Thread.CurrentThread.Join(IterationsDelayBetween);
+                    await Task.Delay(IterationsDelayBetween);
+                    //System.Threading.Thread.CurrentThread.Join(IterationsDelayBetween);
 
 
+                if (_isIterationRunning && !AllowOverlap)
+                {
+                    await Task.Delay(100);
+                    continue;
+                }
+                _isIterationRunning = true;
                 if (IterationsLimit == 0)
                 if (IterationsLimit == 0)
                 {
                 {
                     if (_customIteration != null)
                     if (_customIteration != null)
@@ -71,6 +84,7 @@ namespace Quadarax.Foundation.Core.Thread
                     if (_cntLimit > IterationsLimit)
                     if (_cntLimit > IterationsLimit)
                         exit = true;
                         exit = true;
                 }
                 }
+                _isIterationRunning = false;
             }
             }
         }
         }
 #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
 #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously

+ 3 - 2
qdr.fnd.core/releasenotes.md

@@ -2,14 +2,15 @@
 Ordered by version descending
 Ordered by version descending
 
 
 ## 0.0.2.0
 ## 0.0.2.0
-Date:__3.10.2023__
+Date:__4.10.2023__
 - extends Thread/LoopWorker to support external ansync iteration body expression
 - extends Thread/LoopWorker to support external ansync iteration body expression
 - fix Thread/ContiguousWorker to support external ansync iteration body expression
 - fix Thread/ContiguousWorker to support external ansync iteration body expression
 - add Thread/BasicWorkerContext and extends IWorkerContext to stores CancellationToken
 - add Thread/BasicWorkerContext and extends IWorkerContext to stores CancellationToken
 - extends Thread/AbstractWorker to work with default IWorkerContext, make public AbstractWorker.Context
 - extends Thread/AbstractWorker to work with default IWorkerContext, make public AbstractWorker.Context
 - add Json/Extensions/JsonDocumentExt.Diff function to compare two JsonDocument
 - add Json/Extensions/JsonDocumentExt.Diff function to compare two JsonDocument
 - extends Logging/ConsoleLogger to support exclude filter in initialization
 - extends Logging/ConsoleLogger to support exclude filter in initialization
-- add Collections/LockedList to support thread safe list
+- add Collections/LockedList to support thread safe listng
+- extends Thread/LoopWorker to support custom overlaping iterations
 - fix quality CA
 - fix quality CA
 - minor fixes
 - minor fixes
 ---
 ---