Minor cleanup TINKERPOP-1774

Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/1c7a3def
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/1c7a3def
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/1c7a3def

Branch: refs/heads/TINKERPOP-1774
Commit: 1c7a3def7e483afc19ff6060e1b8206dc15b062d
Parents: d02628b
Author: Florian Hockmann <[email protected]>
Authored: Fri Aug 31 18:17:31 2018 +0200
Committer: Florian Hockmann <[email protected]>
Committed: Fri Aug 31 19:02:24 2018 +0200

----------------------------------------------------------------------
 .../Gremlin.Net/Driver/AsyncAutoResetEvent.cs   | 27 +++++++++++---------
 1 file changed, 15 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/1c7a3def/gremlin-dotnet/src/Gremlin.Net/Driver/AsyncAutoResetEvent.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/AsyncAutoResetEvent.cs 
b/gremlin-dotnet/src/Gremlin.Net/Driver/AsyncAutoResetEvent.cs
index 52c07b0..873f6a7 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/AsyncAutoResetEvent.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/AsyncAutoResetEvent.cs
@@ -25,7 +25,7 @@ using System;
 using System.Collections.Generic;
 using System.Threading.Tasks;
 
-// The implementation is based on this blog post by Stephen Toub:
+// The implementation of this class is inspired by this blog post from Stephen 
Toub:
 // 
https://blogs.msdn.microsoft.com/pfxteam/2012/02/11/building-async-coordination-primitives-part-2-asyncautoresetevent/
 
 namespace Gremlin.Net.Driver
@@ -42,25 +42,19 @@ namespace Gremlin.Net.Driver
         /// <summary>
         ///     Asynchronously waits for this event to be set or until a 
timeout occurs.
         /// </summary>
-        /// <param name="timeout">A <see cref="TimeSpan"/> that that 
represents the number of milliseconds to wait.</param>
+        /// <param name="timeout">A <see cref="TimeSpan"/> that represents the 
number of milliseconds to wait.</param>
         /// <returns>true if the current instance received a signal before 
timing out; otherwise, false.</returns>
         public async Task<bool> WaitOneAsync(TimeSpan timeout)
         {
             var tcs = new TaskCompletionSource<bool>();
             var waitTask = WaitForSignalAsync(tcs);
-            if (waitTask.IsCompleted) return true;
+            if (waitTask.IsCompleted) return waitTask.Result;
             
             await Task.WhenAny(waitTask, 
Task.Delay(timeout)).ConfigureAwait(false);
-            lock (_waitingTasks)
-            {
-                if (!waitTask.IsCompleted)
-                {
-                    // The wait timed out, so we need to remove the waiting 
task.
-                    _waitingTasks.Remove(tcs);
-                    tcs.SetResult(false);
-                }
-            }
+            if (waitTask.IsCompleted) return waitTask.Result;
             
+            StopWaiting(tcs);
+
             return waitTask.Result;
         }
 
@@ -77,6 +71,15 @@ namespace Gremlin.Net.Driver
             }
             return tcs.Task;
         }
+
+        private void StopWaiting(TaskCompletionSource<bool> tcs)
+        {
+            lock (_waitingTasks)
+            {
+                _waitingTasks.Remove(tcs);
+                tcs.SetResult(false);
+            }
+        }
         
         /// <summary>
         ///     Sets the event.

Reply via email to