Browse Source

Cleaned up TaskHelper

tags/docs-0.9
RogueException 9 years ago
parent
commit
3c1ba07178
1 changed files with 12 additions and 8 deletions
  1. +12
    -8
      src/Discord.Net/Helpers/TaskHelper.cs

+ 12
- 8
src/Discord.Net/Helpers/TaskHelper.cs 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);
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;
} }
} }
} }


Loading…
Cancel
Save