/
dev-npgsql
/
npgsql
Обзор
Документация
Войти
/
dev-npgsql
/
npgsql
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
v10.0.0-rc.1
src/Npgsql/Util/TaskSchedulerAwaitable.cs
35 строк
1 KB
Shay Rojansky
Apply primary constructors to solution (#5908)
30 окт 2024, 18:30
Не верифицирован
30 окт 2024, 18:30
40cc1a1
Код
Авторство
О чём код?
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; }