forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskExtensions.cs
More file actions
104 lines (98 loc) · 4.64 KB
/
TaskExtensions.cs
File metadata and controls
104 lines (98 loc) · 4.64 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Npgsql
{
internal static class TaskExtensions
{
/// <summary>
/// Utility that simplifies awaiting a task with a timeout. If the given task does not
/// complete within <paramref name="timeout"/>, a <see cref="TimeoutException"/> is thrown.
/// </summary>
/// <param name="task">The task to be awaited</param>
/// <param name="timeout">How much time to allow <paramref name="task"/> to complete before throwing a <see cref="TimeoutException"/></param>
/// <returns>An awaitable task that represents the original task plus the timeout</returns>
internal static async Task<T> WithTimeout<T>(this Task<T> task, NpgsqlTimeout timeout)
{
if (!timeout.IsSet)
return await task.ConfigureAwait(false);
var timeLeft = timeout.TimeLeft;
if (timeLeft < TimeSpan.Zero)
throw new TimeoutException();
var timeoutTask = Task.Delay(timeLeft);
if (task != await Task.WhenAny(task, timeoutTask).ConfigureAwait(false))
throw new TimeoutException();
return await task.ConfigureAwait(false);
}
/// <summary>
/// Utility that simplifies awaiting a task with a timeout. If the given task does not
/// complete within <paramref name="timeout"/>, a <see cref="TimeoutException"/> is thrown.
/// </summary>
/// <param name="task">The task to be awaited</param>
/// <param name="timeout">How much time to allow <paramref name="task"/> to complete before throwing a <see cref="TimeoutException"/></param>
/// <returns>An awaitable task that represents the original task plus the timeout</returns>
internal static async Task WithTimeout(this Task task, NpgsqlTimeout timeout)
{
if (!timeout.IsSet)
{
await task.ConfigureAwait(false);
return;
}
var timeLeft = timeout.TimeLeft;
if (timeLeft < TimeSpan.Zero)
throw new TimeoutException();
var timeoutTask = Task.Delay(timeLeft);
if (task != await Task.WhenAny(task, timeoutTask).ConfigureAwait(false))
throw new TimeoutException();
await task.ConfigureAwait(false);
}
/// <summary>
/// Allows you to cancel awaiting for a non-cancellable task.
/// </summary>
/// <remarks>
/// Read http://blogs.msdn.com/b/pfxteam/archive/2012/10/05/how-do-i-cancel-non-cancelable-async-operations.aspx
/// and be very careful with this.
/// </remarks>
internal static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
using (cancellationToken.Register(
s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
if (task != await Task.WhenAny(task, tcs.Task).ConfigureAwait(false))
throw new TaskCanceledException(task);
return await task.ConfigureAwait(false);
}
/// <summary>
/// Allows you to cancel awaiting for a non-cancellable task.
/// </summary>
/// <remarks>
/// Read http://blogs.msdn.com/b/pfxteam/archive/2012/10/05/how-do-i-cancel-non-cancelable-async-operations.aspx
/// and be very careful with this.
/// </remarks>
internal static async Task WithCancellation(this Task task, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
using (cancellationToken.Register(
s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
if (task != await Task.WhenAny(task, tcs.Task).ConfigureAwait(false))
throw new TaskCanceledException(task);
await task.ConfigureAwait(false);
}
internal static Task<T> WithCancellationAndTimeout<T>(this Task<T> task, NpgsqlTimeout timeout, CancellationToken cancellationToken)
{
return task
.WithCancellation(cancellationToken)
.WithTimeout(timeout);
}
internal static Task WithCancellationAndTimeout(this Task task, NpgsqlTimeout timeout, CancellationToken cancellationToken)
{
return task
.WithCancellation(cancellationToken)
.WithTimeout(timeout);
}
}
}