diff --git a/lib/PuppeteerSharp/Helpers/TaskHelper.cs b/lib/PuppeteerSharp/Helpers/TaskHelper.cs index 21965adf3..7a6ecaa01 100644 --- a/lib/PuppeteerSharp/Helpers/TaskHelper.cs +++ b/lib/PuppeteerSharp/Helpers/TaskHelper.cs @@ -9,9 +9,14 @@ namespace PuppeteerSharp.Helpers /// public static class TaskHelper { - private static readonly Func DefaultExceptionFactory = + private static readonly Func _defaultExceptionFactory = timeout => new TimeoutException($"Timeout of {timeout.TotalMilliseconds} ms exceeded"); + /// + /// Default timeout. + /// + public static int DefaultTimeout { get; set; } = 1_000; + // Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/ /// @@ -24,10 +29,10 @@ public static class TaskHelper /// Cancellation token. public static Task WithTimeout( this Task task, - int milliseconds = 1_000, + int? milliseconds = null, Func exceptionFactory = null, CancellationToken cancellationToken = default) - => WithTimeout(task, TimeSpan.FromMilliseconds(milliseconds), exceptionFactory, cancellationToken); + => WithTimeout(task, TimeSpan.FromMilliseconds(milliseconds ?? DefaultTimeout), exceptionFactory, cancellationToken); // Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/ @@ -45,7 +50,7 @@ public static class TaskHelper Func exceptionFactory = null, CancellationToken cancellationToken = default) => task.WithTimeout( - () => throw (exceptionFactory ?? DefaultExceptionFactory)(timeout), + () => throw (exceptionFactory ?? _defaultExceptionFactory)(timeout), timeout, cancellationToken); @@ -155,8 +160,8 @@ public static async Task WithTimeout(this Task task, Action timeoutActi /// Milliseconds timeout. /// Optional timeout exception factory. /// Task return type. - public static Task WithTimeout(this Task task, int milliseconds = 1_000, Func exceptionFactory = null) - => WithTimeout(task, TimeSpan.FromMilliseconds(milliseconds), exceptionFactory); + public static Task WithTimeout(this Task task, int? milliseconds = null, Func exceptionFactory = null) + => WithTimeout(task, TimeSpan.FromMilliseconds(milliseconds ?? DefaultTimeout), exceptionFactory); // Recipe from https://blogs.msdn.microsoft.com/pfxteam/2012/10/05/how-do-i-cancel-non-cancelable-async-operations/ @@ -177,7 +182,7 @@ public static async Task WithTimeout(this Task task, TimeSpan timeout, if (await TimeoutTask(task, timeout).ConfigureAwait(false)) { - throw (exceptionFactory ?? DefaultExceptionFactory)(timeout); + throw (exceptionFactory ?? _defaultExceptionFactory)(timeout); } return await task.ConfigureAwait(false);