|
| 1 | +using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace ThreadingTest; |
| 7 | + |
| 8 | +[SuppressMessage("Async/await", "CRR0038:CancellationToken parameter is never used.", Justification = "<Pending>")] |
| 9 | +[SuppressMessage( |
| 10 | + "Minor Code Smell", |
| 11 | + "S4261:Methods should be named according to their synchronicities", |
| 12 | + Justification = "<Pending>")] |
| 13 | +public static class TaskFactoryExtensions |
| 14 | +{ |
| 15 | + |
| 16 | + #region Constants & Statics |
| 17 | + |
| 18 | + public static Task<TResult> StartNewUnwrapped<TResult>(this TaskFactory factory, Func<Task<TResult>> function) |
| 19 | + { |
| 20 | + var task = factory.StartNew(function); |
| 21 | + |
| 22 | + return task.Unwrap(); |
| 23 | + } |
| 24 | + |
| 25 | + public static Task<TResult> StartNewUnwrapped<TResult>( |
| 26 | + this TaskFactory factory, |
| 27 | + Func<Task<TResult>> function, |
| 28 | + CancellationToken cancellationToken) |
| 29 | + { |
| 30 | + var task = factory.StartNew(function, cancellationToken); |
| 31 | + |
| 32 | + return task.Unwrap(); |
| 33 | + } |
| 34 | + |
| 35 | + public static Task<TResult> StartNewUnwrapped<TResult>( |
| 36 | + this TaskFactory factory, |
| 37 | + Func<Task<TResult>> function, |
| 38 | + TaskCreationOptions creationOptions) |
| 39 | + { |
| 40 | + var task = factory.StartNew(function, creationOptions); |
| 41 | + |
| 42 | + return task.Unwrap(); |
| 43 | + } |
| 44 | + |
| 45 | + public static Task<TResult> StartNewUnwrapped<TResult>( |
| 46 | + this TaskFactory factory, |
| 47 | + Func<object?, Task<TResult>> function, |
| 48 | + object? state) |
| 49 | + { |
| 50 | + var task = factory.StartNew(function, state); |
| 51 | + |
| 52 | + return task.Unwrap(); |
| 53 | + } |
| 54 | + |
| 55 | + public static Task<TResult> StartNewUnwrapped<TResult>( |
| 56 | + this TaskFactory factory, |
| 57 | + Func<object?, Task<TResult>> function, |
| 58 | + object? state, |
| 59 | + CancellationToken cancellationToken) |
| 60 | + { |
| 61 | + var task = factory.StartNew(function, state, cancellationToken); |
| 62 | + |
| 63 | + return task.Unwrap(); |
| 64 | + } |
| 65 | + |
| 66 | + public static Task<TResult> StartNewUnwrapped<TResult>( |
| 67 | + this TaskFactory factory, |
| 68 | + Func<object?, Task<TResult>> function, |
| 69 | + object? state, |
| 70 | + TaskCreationOptions creationOptions) |
| 71 | + { |
| 72 | + var task = factory.StartNew(function, state, creationOptions); |
| 73 | + |
| 74 | + return task.Unwrap(); |
| 75 | + } |
| 76 | + |
| 77 | + public static Task<TResult> StartNewUnwrapped<TResult>( |
| 78 | + this TaskFactory factory, |
| 79 | + Func<Task<TResult>> function, |
| 80 | + TaskScheduler scheduler, |
| 81 | + CancellationToken? cancellationToken = null, |
| 82 | + TaskCreationOptions? creationOptions = null) |
| 83 | + { |
| 84 | + var task = factory.StartNew( |
| 85 | + function, |
| 86 | + cancellationToken ?? factory.CancellationToken, |
| 87 | + creationOptions ?? factory.CreationOptions, |
| 88 | + scheduler); |
| 89 | + |
| 90 | + return task.Unwrap(); |
| 91 | + } |
| 92 | + |
| 93 | + public static Task<TResult> StartNewUnwrapped<TResult>( |
| 94 | + this TaskFactory factory, |
| 95 | + Func<object?, Task<TResult>> function, |
| 96 | + object? state, |
| 97 | + TaskScheduler scheduler, |
| 98 | + CancellationToken? cancellationToken = null, |
| 99 | + TaskCreationOptions? creationOptions = null) |
| 100 | + { |
| 101 | + var task = factory.StartNew( |
| 102 | + function, |
| 103 | + state, |
| 104 | + cancellationToken ?? factory.CancellationToken, |
| 105 | + creationOptions ?? factory.CreationOptions, |
| 106 | + scheduler); |
| 107 | + |
| 108 | + return task.Unwrap(); |
| 109 | + } |
| 110 | + |
| 111 | + #endregion |
| 112 | + |
| 113 | +} |
0 commit comments