From e7245732ddae9222283fec370a96370036ba7a08 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 12 Nov 2026 18:25:17 -0600 Subject: add Cancel extension --- src/slskd/Common/CommonExtensions.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index 47e4aee1..5affdf5c 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -230,6 +230,25 @@ namespace slskd } } + /// + /// Safely cancels this without throwing if it is disposed. + /// + /// The CancellationTokenSource instance. + /// A value indicating whether to swallow ObjectDisposedException, if it occurs. + /// A value indicating whether the cancellation completed successfully. + public static bool Cancel(this CancellationTokenSource cts, bool swallowObjectDisposedException) + { + try + { + cts.Cancel(); + return true; + } + catch (ObjectDisposedException) when (swallowObjectDisposedException) + { + return false; + } + } + /// /// Recursively retrieves all properties. /// -- cgit v1.2.3