-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathTaskSchedulerAwaitable.cs
More file actions
35 lines (28 loc) · 1.24 KB
/
TaskSchedulerAwaitable.cs
File metadata and controls
35 lines (28 loc) · 1.24 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
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Npgsql.Util;
readonly struct TaskSchedulerAwaitable(TaskScheduler scheduler) : ICriticalNotifyCompletion
{
public void GetResult() {}
public bool IsCompleted => false;
public void OnCompleted(Action continuation)
{
var task = Task.Factory.StartNew(continuation, CancellationToken.None,
TaskCreationOptions.DenyChildAttach,
scheduler: scheduler);
// Exceptions should never happen as the continuation should be the async statemachine.
// It normally does its own error handling through the returned task unless it's an async void returning method.
// In which case we should absolutely let it bubble up to TaskScheduler.UnobservedTaskException.
OnFaulted(task);
[Conditional("DEBUG")]
static void OnFaulted(Task task)
{
task.ContinueWith(t => Debug.Fail("Task scheduler task threw an unobserved exception"), TaskContinuationOptions.OnlyOnFaulted);
}
}
public void UnsafeOnCompleted(Action continuation) => OnCompleted(continuation);
public TaskSchedulerAwaitable GetAwaiter() => this;
}