diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-01-08 15:56:25 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2023-01-08 15:56:25 -0600 |
| commit | 146928eed2375cb8b8f8175d1957e0ceb1e3128e (patch) | |
| tree | f2a7de658aeac139e24d8c7b6963cf88edd59dfa /src | |
| parent | 8943bd3cecd25897cd68188975b79b7d4d765e36 (diff) | |
| download | slskd-146928eed2375cb8b8f8175d1957e0ceb1e3128e.tar.gz | |
add TimeoutAfter() extensions
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/CommonExtensions.cs | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index 3dddd3b4..d18a88e1 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -23,12 +23,12 @@ namespace slskd using System.IdentityModel.Tokens.Jwt; using System.IO; using System.Linq; - using System.Numerics; using System.Reflection; - using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; + using System.Threading; + using System.Threading.Tasks; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; @@ -38,6 +38,55 @@ namespace slskd public static class CommonExtensions { /// <summary> + /// Times this Task out after the specified TimeSpan. + /// </summary> + /// <typeparam name="T">The result type.</typeparam> + /// <param name="task">The task.</param> + /// <param name="timeout">The timeout.</param> + /// <returns>The operation context.</returns> + /// <exception cref="TimeoutException">Thrown when the task times out.</exception> + public static async Task<T> TimeoutAfter<T>(this Task<T> task, TimeSpan timeout) + { + using var timeoutCancellationTokenSource = new CancellationTokenSource(); + + var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token)); + + if (completedTask == task) + { + timeoutCancellationTokenSource.Cancel(); + return await task; + } + else + { + throw new TimeoutException("The operation has timed out."); + } + } + + /// <summary> + /// Times this Task out after the specified TimeSpan. + /// </summary> + /// <typeparam name="T">The result type.</typeparam> + /// <param name="task">The task.</param> + /// <param name="timeout">The timeout.</param> + /// <exception cref="TimeoutException">Thrown when the task times out.</exception> + public static async Task TimeoutAfter(this Task task, TimeSpan timeout) + { + using var timeoutCancellationTokenSource = new CancellationTokenSource(); + + var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token)); + + if (completedTask == task) + { + timeoutCancellationTokenSource.Cancel(); + await task; + } + else + { + throw new TimeoutException("The operation has timed out."); + } + } + + /// <summary> /// Deeply compares this object with the specified object and returns a list of properties that are different. /// </summary> /// <param name="left">The left side of the comparison.</param> |
