| @@ -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); | |||||
| await self; | |||||
| timeoutToken.CancelAfter(milliseconds); | |||||
| 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); | |||||
| return await self; | |||||
| timeoutToken.CancelAfter(milliseconds); | |||||
| return await self.ConfigureAwait(false); | |||||
| } | } | ||||
| catch (OperationCanceledException) | catch (OperationCanceledException) | ||||
| { | { | ||||
| throw new TimeoutException(); | |||||
| if (timeoutToken.IsCancellationRequested) | |||||
| throw new TimeoutException(); | |||||
| throw; | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||