forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskExtensions.cs
More file actions
52 lines (41 loc) · 1.78 KB
/
TaskExtensions.cs
File metadata and controls
52 lines (41 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using ModelContextProtocol;
namespace System.Threading.Tasks;
internal static class TaskExtensions
{
public static Task WaitAsync(this Task task, CancellationToken cancellationToken)
{
return WaitAsync(task, Timeout.InfiniteTimeSpan, cancellationToken);
}
public static Task<T> WaitAsync<T>(this Task<T> task, CancellationToken cancellationToken)
{
return WaitAsync(task, Timeout.InfiniteTimeSpan, cancellationToken);
}
public static async Task<T> WaitAsync<T>(this Task<T> task, TimeSpan timeout, CancellationToken cancellationToken = default)
{
await WaitAsync((Task)task, timeout, cancellationToken).ConfigureAwait(false);
return task.Result;
}
public static async Task WaitAsync(this Task task, TimeSpan timeout, CancellationToken cancellationToken = default)
{
Throw.IfNull(task);
if (timeout < TimeSpan.Zero && timeout != Timeout.InfiniteTimeSpan)
{
throw new ArgumentOutOfRangeException(nameof(timeout));
}
if (!task.IsCompleted)
{
cancellationToken.ThrowIfCancellationRequested();
using CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(timeout);
var cancellationTask = new TaskCompletionSource<bool>();
using var _ = cts.Token.Register(tcs => ((TaskCompletionSource<bool>)tcs!).TrySetResult(true), cancellationTask);
await Task.WhenAny(task, cancellationTask.Task).ConfigureAwait(false);
if (!task.IsCompleted)
{
cancellationToken.ThrowIfCancellationRequested();
throw new TimeoutException();
}
}
await task.ConfigureAwait(false);
}
}