From 146928eed2375cb8b8f8175d1957e0ceb1e3128e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 8 Jan 2023 15:56:25 -0600 Subject: add TimeoutAfter() extensions --- src/slskd/Common/CommonExtensions.cs | 53 ++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'src/slskd/Common/CommonExtensions.cs') 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; @@ -37,6 +37,55 @@ namespace slskd /// public static class CommonExtensions { + /// + /// Times this Task out after the specified TimeSpan. + /// + /// The result type. + /// The task. + /// The timeout. + /// The operation context. + /// Thrown when the task times out. + 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(); + return await task; + } + else + { + throw new TimeoutException("The operation has timed out."); + } + } + + /// + /// Times this Task out after the specified TimeSpan. + /// + /// The result type. + /// The task. + /// The timeout. + /// Thrown when the task times out. + 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."); + } + } + /// /// Deeply compares this object with the specified object and returns a list of properties that are different. /// -- cgit v1.2.3 From fba17ebb02ae0fafe028c4cf88bb988d437bf44a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 8 Jan 2023 16:20:58 -0600 Subject: warnings --- src/slskd/Common/CommonExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Common/CommonExtensions.cs') diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index d18a88e1..6a472b9b 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -65,9 +65,9 @@ namespace slskd /// /// Times this Task out after the specified TimeSpan. /// - /// The result type. /// The task. /// The timeout. + /// The operation context. /// Thrown when the task times out. public static async Task TimeoutAfter(this Task task, TimeSpan timeout) { -- cgit v1.2.3