diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-11-12 18:25:17 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-11-12 18:25:17 -0600 |
| commit | e7245732ddae9222283fec370a96370036ba7a08 (patch) | |
| tree | f670150a3ec93c0c84cb67d358a03c228e79ed3a /src | |
| parent | 2622c9b698fa27ebfcc7239b75334cac96b7e88a (diff) | |
| download | slskd-e7245732ddae9222283fec370a96370036ba7a08.tar.gz | |
add Cancel extension
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/CommonExtensions.cs | 19 |
1 files changed, 19 insertions, 0 deletions
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 @@ -231,6 +231,25 @@ namespace slskd } /// <summary> + /// Safely cancels this <see cref="CancellationTokenSource"/> without throwing if it is disposed. + /// </summary> + /// <param name="cts">The CancellationTokenSource instance.</param> + /// <param name="swallowObjectDisposedException">A value indicating whether to swallow ObjectDisposedException, if it occurs.</param> + /// <returns>A value indicating whether the cancellation completed successfully.</returns> + public static bool Cancel(this CancellationTokenSource cts, bool swallowObjectDisposedException) + { + try + { + cts.Cancel(); + return true; + } + catch (ObjectDisposedException) when (swallowObjectDisposedException) + { + return false; + } + } + + /// <summary> /// Recursively retrieves all properties. /// </summary> /// <param name="type">The type from which to retrieve properties.</param> |
