diff --git a/CSRedis/Internal/RedisAsyncCommandToken.cs b/CSRedis/Internal/RedisAsyncCommandToken.cs index c2323cd..0636f61 100644 --- a/CSRedis/Internal/RedisAsyncCommandToken.cs +++ b/CSRedis/Internal/RedisAsyncCommandToken.cs @@ -1,43 +1,31 @@ using CSRedis.Internal.IO; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; namespace CSRedis.Internal { interface IRedisAsyncCommandToken { - Task Task { get; } RedisCommand Command { get; } void SetResult(RedisReader reader); void SetException(Exception e); + bool TrySetException(Exception e); } - class RedisAsyncCommandToken : IRedisAsyncCommandToken + class RedisAsyncCommandToken : TaskCompletionSource, IRedisAsyncCommandToken { - readonly TaskCompletionSource _tcs; readonly RedisCommand _command; - public TaskCompletionSource TaskSource { get { return _tcs; } } public RedisCommand Command { get { return _command; } } - public Task Task { get { return _tcs.Task; } } public RedisAsyncCommandToken(RedisCommand command) { - _tcs = new TaskCompletionSource(); _command = command; } public void SetResult(RedisReader reader) { - _tcs.SetResult(_command.Parse(reader)); - } - - public void SetException(Exception e) - { - _tcs.SetException(e); + SetResult(_command.Parse(reader)); } } } diff --git a/CSRedis/Internal/RedisConnector.cs b/CSRedis/Internal/RedisConnector.cs index f960ea8..870ac7b 100644 --- a/CSRedis/Internal/RedisConnector.cs +++ b/CSRedis/Internal/RedisConnector.cs @@ -135,7 +135,7 @@ public Task CallAsync(RedisCommand command) var token = new RedisAsyncCommandToken(command); AsyncWriteQueue.Enqueue(token); ConnectAsync().ContinueWith(CallAsyncDeferred); - return token.TaskSource.Task; + return token.Task; } public void Write(RedisCommand command) @@ -316,7 +316,7 @@ void OnSocketSent(SocketAsyncEventArgs args) } catch (Exception e) { - token.SetException(e); + token.TrySetException(e); } } }