Cleaned up TaskHelper

This commit is contained in:
RogueException
2015-10-12 03:40:14 -03:00
parent 7c024ecda8
commit 3c1ba07178

View File

@@ -34,28 +34,32 @@ namespace Discord.Helpers
else else
return await self.ConfigureAwait(false); return await self.ConfigureAwait(false);
} }
public static async Task Timeout(this Task self, int milliseconds, CancellationTokenSource cancelToken) public static async Task Timeout(this Task self, int milliseconds, CancellationTokenSource timeoutToken)
{ {
try try
{ {
cancelToken.CancelAfter(milliseconds); timeoutToken.CancelAfter(milliseconds);
await self; await self.ConfigureAwait(false);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
throw new TimeoutException(); if (timeoutToken.IsCancellationRequested)
throw new TimeoutException();
throw;
} }
} }
public static async Task<T> Timeout<T>(this Task<T> self, int milliseconds, CancellationTokenSource cancelToken) public static async Task<T> Timeout<T>(this Task<T> self, int milliseconds, CancellationTokenSource timeoutToken)
{ {
try try
{ {
cancelToken.CancelAfter(milliseconds); timeoutToken.CancelAfter(milliseconds);
return await self; return await self.ConfigureAwait(false);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
throw new TimeoutException(); if (timeoutToken.IsCancellationRequested)
throw new TimeoutException();
throw;
} }
} }
} }