Skip to content

Commit

Permalink
Always dispose cancellation token sources (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
Im5tu authored and kblok committed Feb 21, 2019
1 parent 4271b97 commit 7a1de99
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/PuppeteerSharp/Helpers/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,21 @@ public static async Task<T> WithTimeout<T>(
private static async Task<bool> TimeoutTask(Task task, int milliseconds)
{
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var cancellationToken = new CancellationTokenSource();

if (milliseconds > 0)
{
cancellationToken.CancelAfter(milliseconds);
}
using (cancellationToken.Token.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
using (var cancellationToken = new CancellationTokenSource())
{
if (task != await Task.WhenAny(task, tcs.Task))
if (milliseconds > 0)
{
cancellationToken.CancelAfter(milliseconds);
}
using (cancellationToken.Token.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
{
return true;
if (task != await Task.WhenAny(task, tcs.Task))
{
return true;
}
}
return false;
}
return false;
}
}
}

0 comments on commit 7a1de99

Please sign in to comment.