|
| 1 | +// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +// software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +// without restriction, including without limitation the rights to use, copy, modify, merge, |
| 6 | +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 7 | +// to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +// |
| 9 | +// The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +// substantial portions of the Software. |
| 11 | +// |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 13 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 14 | +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 15 | +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 17 | +// DEALINGS IN THE SOFTWARE. |
| 18 | + |
| 19 | +using System; |
| 20 | +using System.Threading; |
| 21 | +using System.Threading.Tasks; |
| 22 | +using ICSharpCode.ILSpy.TextView; |
| 23 | + |
| 24 | +namespace ICSharpCode.ILSpy |
| 25 | +{ |
| 26 | + public static class TaskHelper |
| 27 | + { |
| 28 | + public static readonly Task CompletedTask = FromResult<object>(null); |
| 29 | + |
| 30 | + public static Task<T> FromResult<T>(T result) |
| 31 | + { |
| 32 | + TaskCompletionSource<T> tcs = new TaskCompletionSource<T>(); |
| 33 | + tcs.SetResult(result); |
| 34 | + return tcs.Task; |
| 35 | + } |
| 36 | + |
| 37 | + public static Task<T> FromException<T>(Exception ex) |
| 38 | + { |
| 39 | + var tcs = new TaskCompletionSource<T>(); |
| 40 | + tcs.SetException(ex); |
| 41 | + return tcs.Task; |
| 42 | + } |
| 43 | + |
| 44 | + public static Task<T> FromCancellation<T>() |
| 45 | + { |
| 46 | + var tcs = new TaskCompletionSource<T>(); |
| 47 | + tcs.SetCanceled(); |
| 48 | + return tcs.Task; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Sets the result of the TaskCompletionSource based on the result of the finished task. |
| 53 | + /// </summary> |
| 54 | + public static void SetFromTask<T>(this TaskCompletionSource<T> tcs, Task<T> task) |
| 55 | + { |
| 56 | + switch (task.Status) { |
| 57 | + case TaskStatus.RanToCompletion: |
| 58 | + tcs.SetResult(task.Result); |
| 59 | + break; |
| 60 | + case TaskStatus.Canceled: |
| 61 | + tcs.SetCanceled(); |
| 62 | + break; |
| 63 | + case TaskStatus.Faulted: |
| 64 | + tcs.SetException(task.Exception.InnerExceptions); |
| 65 | + break; |
| 66 | + default: |
| 67 | + throw new InvalidOperationException("The input task must have already finished"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Sets the result of the TaskCompletionSource based on the result of the finished task. |
| 73 | + /// </summary> |
| 74 | + public static void SetFromTask(this TaskCompletionSource<object> tcs, Task task) |
| 75 | + { |
| 76 | + switch (task.Status) { |
| 77 | + case TaskStatus.RanToCompletion: |
| 78 | + tcs.SetResult(null); |
| 79 | + break; |
| 80 | + case TaskStatus.Canceled: |
| 81 | + tcs.SetCanceled(); |
| 82 | + break; |
| 83 | + case TaskStatus.Faulted: |
| 84 | + tcs.SetException(task.Exception.InnerExceptions); |
| 85 | + break; |
| 86 | + default: |
| 87 | + throw new InvalidOperationException("The input task must have already finished"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static Task Then<T>(this Task<T> task, Action<T> action) |
| 92 | + { |
| 93 | + if (action == null) |
| 94 | + throw new ArgumentNullException("action"); |
| 95 | + return task.ContinueWith(t => action(t.Result), CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.FromCurrentSynchronizationContext()); |
| 96 | + } |
| 97 | + |
| 98 | + public static Task<U> Then<T, U>(this Task<T> task, Func<T, U> func) |
| 99 | + { |
| 100 | + if (func == null) |
| 101 | + throw new ArgumentNullException("func"); |
| 102 | + return task.ContinueWith(t => func(t.Result), CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.FromCurrentSynchronizationContext()); |
| 103 | + } |
| 104 | + |
| 105 | + public static Task Then<T>(this Task<T> task, Func<T, Task> asyncFunc) |
| 106 | + { |
| 107 | + if (asyncFunc == null) |
| 108 | + throw new ArgumentNullException("asyncFunc"); |
| 109 | + return task.ContinueWith(t => asyncFunc(t.Result), CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap(); |
| 110 | + } |
| 111 | + |
| 112 | + public static Task<U> Then<T, U>(this Task<T> task, Func<T, Task<U>> asyncFunc) |
| 113 | + { |
| 114 | + if (asyncFunc == null) |
| 115 | + throw new ArgumentNullException("asyncFunc"); |
| 116 | + return task.ContinueWith(t => asyncFunc(t.Result), CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap(); |
| 117 | + } |
| 118 | + |
| 119 | + public static Task Then(this Task task, Action action) |
| 120 | + { |
| 121 | + if (action == null) |
| 122 | + throw new ArgumentNullException("action"); |
| 123 | + return task.ContinueWith(t => { |
| 124 | + t.Wait(); |
| 125 | + action(); |
| 126 | + }, CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.FromCurrentSynchronizationContext()); |
| 127 | + } |
| 128 | + |
| 129 | + public static Task<U> Then<U>(this Task task, Func<U> func) |
| 130 | + { |
| 131 | + if (func == null) |
| 132 | + throw new ArgumentNullException("func"); |
| 133 | + return task.ContinueWith(t => { |
| 134 | + t.Wait(); |
| 135 | + return func(); |
| 136 | + }, CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.FromCurrentSynchronizationContext()); |
| 137 | + } |
| 138 | + |
| 139 | + public static Task Then(this Task task, Func<Task> asyncAction) |
| 140 | + { |
| 141 | + if (asyncAction == null) |
| 142 | + throw new ArgumentNullException("asyncAction"); |
| 143 | + return task.ContinueWith(t => { |
| 144 | + t.Wait(); |
| 145 | + return asyncAction(); |
| 146 | + }, CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap(); |
| 147 | + } |
| 148 | + |
| 149 | + public static Task<U> Then<U>(this Task task, Func<Task<U>> asyncFunc) |
| 150 | + { |
| 151 | + if (asyncFunc == null) |
| 152 | + throw new ArgumentNullException("asyncFunc"); |
| 153 | + return task.ContinueWith(t => { |
| 154 | + t.Wait(); |
| 155 | + return asyncFunc(); |
| 156 | + }, CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.FromCurrentSynchronizationContext()).Unwrap(); |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// If the input task fails, calls the action to handle the error. |
| 161 | + /// </summary> |
| 162 | + /// <returns> |
| 163 | + /// Returns a task that finishes successfully when error handling has completed. |
| 164 | + /// If the input task ran successfully, the returned task completes successfully. |
| 165 | + /// If the input task was cancelled, the returned task is cancelled as well. |
| 166 | + /// </returns> |
| 167 | + public static Task Catch<TException>(this Task task, Action<TException> action) where TException : Exception |
| 168 | + { |
| 169 | + if (action == null) |
| 170 | + throw new ArgumentNullException("action"); |
| 171 | + return task.ContinueWith(t => { |
| 172 | + if (t.IsFaulted) { |
| 173 | + Exception ex = t.Exception; |
| 174 | + while (ex is AggregateException) |
| 175 | + ex = ex.InnerException; |
| 176 | + if (ex is TException) |
| 177 | + action((TException)ex); |
| 178 | + else |
| 179 | + throw t.Exception; |
| 180 | + } |
| 181 | + }, CancellationToken.None, TaskContinuationOptions.NotOnCanceled, TaskScheduler.FromCurrentSynchronizationContext()); |
| 182 | + } |
| 183 | + |
| 184 | + /// <summary> |
| 185 | + /// Ignore exceptions thrown by the task. |
| 186 | + /// </summary> |
| 187 | + public static void IgnoreExceptions(this Task task) |
| 188 | + { |
| 189 | + } |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// Handle exceptions by displaying the error message in the text view. |
| 193 | + /// </summary> |
| 194 | + public static void HandleExceptions(this Task task) |
| 195 | + { |
| 196 | + task.Catch<Exception>(exception => MainWindow.Instance.Dispatcher.BeginInvoke(new Action(delegate { |
| 197 | + AvalonEditTextOutput output = new AvalonEditTextOutput(); |
| 198 | + output.Write(exception.ToString()); |
| 199 | + MainWindow.Instance.TextView.ShowText(output); |
| 200 | + }))).IgnoreExceptions(); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments