Skip to content
Permalink
Browse files

Always dispose cancellation token sources (#926)

  • Loading branch information...
Im5tu authored and kblok committed Feb 21, 2019
1 parent 4271b97 commit 7a1de994ccd6325122020c7af95d1898b71e359d
Showing with 11 additions and 10 deletions.
  1. +11 −10 lib/PuppeteerSharp/Helpers/TaskHelper.cs
@@ -93,20 +93,21 @@ public static class TaskHelper
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.
You can’t perform that action at this time.