[smuxi: 2/37] ThreadPoolQueue: refactoring, extract Dequeue() from CheckQueue()




commit d9f183deefb37701ddf74953016b3a43d83304f2
Author: Andres G. Aragoneses <knocte gmail com>
Date:   Thu Oct 13 22:06:51 2016 +0800

    ThreadPoolQueue: refactoring, extract Dequeue() from CheckQueue()
    
    This way:
    * CheckQueue() is not so long.
    * We don't need to assign `default(Action)` dummy useless
    initialization value to the variable `action`.
    * It's more consistent as there was already another
    method called `Enqueue`

 src/Common/ThreadPoolQueue.cs | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
---
diff --git a/src/Common/ThreadPoolQueue.cs b/src/Common/ThreadPoolQueue.cs
index d78bd580..af874ade 100644
--- a/src/Common/ThreadPoolQueue.cs
+++ b/src/Common/ThreadPoolQueue.cs
@@ -55,18 +55,11 @@ namespace Smuxi.Common
 
         void CheckQueue()
         {
-            var action = default(Action);
-            lock (ActionQueue) {
-                if (ActionQueue.Count == 0) {
-                    return;
-                }
-
-                if (ActiveWorkers >= MaxWorkers) {
-                    return;
-                }
-
-                action = ActionQueue.Dequeue();
+            var action = Dequeue();
+            if (action == null) {
+                return;
             }
+
             Interlocked.Increment(ref ActiveWorkers);
 
             ThreadPool.QueueUserWorkItem(delegate {
@@ -78,5 +71,20 @@ namespace Smuxi.Common
                 }
             });
         }
+
+        Action Dequeue()
+        {
+            lock (ActionQueue) {
+                if (ActionQueue.Count == 0) {
+                    return null;
+                }
+
+                if (ActiveWorkers >= MaxWorkers) {
+                    return null;
+                }
+
+                return ActionQueue.Dequeue();
+            }
+        }
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]