UnobservedTaskException being throw but it is handled by a TaskScheduler.UnobservedTaskException handler and a continuations OnlyOnFaulted handler
本问题已经有最佳答案,请猛点这里访问。
我遇到TPL编程问题。
我正在使用@h4165f8ghd4f854d6f8h解决方案获取UnobservedTaskException [http://stackoverflow.com/questions/7883052/a-tasks-exceptions-were-not-observed-either-by-waiting-on-the-task-or-accessi / 11830087#11830087]处理异常但仍然得到UnobservedTaskException。
我在启动任务之前也放了以下代码:
1 2 3 4 5 | TaskScheduler.UnobservedTaskException += (sender, e) => { e.SetObserved(); throw e.Exception; }; |
但[http://stackoverflow.com/questions/10874068/exception-thrown-in-task-thread-not-caught-by-unobservedtaskexception]告诉它不要捕获每个TPL未处理的异常。
我想要泡沫异常,直到达到堆栈顶部然后处理它。
有人可以帮帮我????
@Jon Skeet
嗨,我做得更小了
它现在编辑
谢谢你看
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 | class Program { static void Main(string[] args) { Program p = new Program(); p.tplTestOne(); } public void tplTestOne() { TaskScheduler.UnobservedTaskException += (sender, e) => { e.SetObserved(); throw e.Exception; }; Task tsk_1 = MyClassHere.createHandledTask(() => { double x = 1; x = (x + 1) / x; }, false); Task tsk_2 = MyClassHere.createHandledTask(() => { double y = 0; throw new Exception("forced_divisionbyzerodontthrowanymore_test"); // here -> System.Exception was unhandled by user code }, true); Task tsk_3 = MyClassHere.createHandledTask(() => { double z = 1; z = (z + 1) / z; }, true); Task tsk_4 = MyClassHere.createHandledTask(() => { double k = 1; k = (k + 1) / k; }, true); Console.ReadLine(); } } public static class MyClassHere { public static void waitForTsk(Task t) { try { t.Wait(); } catch (AggregateException ae) { ae.Handle((err) => { throw err; }); } } public static void throwFirstExceptionIfHappens(this Task task) { task.ContinueWith(t => { var aggException = t.Exception.Flatten(); foreach (var exception in aggException.InnerExceptions) { throw exception; // throw only first, search for solution } }, TaskContinuationOptions.OnlyOnFaulted); // not valid for multi task continuations } public static Task createHandledTask(Action action) { return createHandledTask(action, false); } public static Task createHandledTask(Action action, bool attachToParent) { Task tsk = null; if (attachToParent) { TaskCreationOptions atp = TaskCreationOptions.AttachedToParent; tsk = Task.Factory.StartNew(action, atp); } else { tsk = Task.Factory.StartNew(action); } tsk.throwFirstExceptionIfHappens(); return tsk; } } |
谢谢
解决方案基于如何在使用任务并行库时处理所有未处理的异常?
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | class Program { static void Main(string[] args) { Program p = new Program(); p.tplTestOne(); } public void tplTestOne() { //------------------------------------------------- MyClassHere.onUnobservedTaskException += (object sender, EventException e) => { Console.WriteLine(e.Exception.Message); //its fired OK }; TaskScheduler.UnobservedTaskException += (object sender, UnobservedTaskExceptionEventArgs e) => { Console.WriteLine(e.Exception.Message); // its not fired, buggy }; //------------------------------------------------- CancellationTokenSource source = new CancellationTokenSource(); Task tz = MyClassHere.CreateHandledTask( new TaskScheduled(0, () => { if (!source.IsCancellationRequested) { Console.WriteLine("A-main-task-started"); } Thread.Sleep(5000); if (source.IsCancellationRequested) { Console.WriteLine("CancelingMainTask"); } }) , new TaskScheduled(3000, () => { Console.WriteLine("okTaskCalled"); }) , null //new TaskScheduled(0, () => { Console.WriteLine("cancelTaskCalled"); }) , TaskCreationOptions.AttachedToParent , source.Token , new TaskScheduled(2000, () => { if (!source.IsCancellationRequested) { Console.WriteLine("B-timeout"); } }) , new TaskScheduled(1000, () => { if (!source.IsCancellationRequested) { Console.WriteLine("C-timeout"); } source.Cancel(); }) ); if(tz != null) { tz.ContinueWith(t => { Console.WriteLine("END"); }); } Task tsk_1 = MyClassHere.createHandledTask(() => { double x = 1; x = (x + 1) / x; }, false); Task tsk_2 = MyClassHere.createHandledTask(() => { double y = 0; throw new Exception("forced_divisionbyzerodontthrowanymore_test"); // here -> System.Exception was unhandled by user code }, true); Task tsk_3 = MyClassHere.createHandledTask(() => { double z = 1; z = (z + 1) / z; }, true); Task tsk_4 = MyClassHere.createHandledTask(() => { double k = 1; k = (k + 1) / k; }, true); Console.ReadLine(); } } public class EventException : EventArgs { public Exception Exception; public Task task; public EventException(Exception err, Task tsk) { Exception = err; task = tsk; } } public class TaskScheduled { public int waitTime; public Action action; public DateTime datestamp; public bool isCalled = false; public TaskScheduled(int _waitTime, Action _action) { this.waitTime = _waitTime; this.action = _action; } } public static class MyClassHere { public delegate void UnobservedTaskException(object sender, EventException e); public static event UnobservedTaskException onUnobservedTaskException; //------------------------------------------------- public static void waitForTsk(Task t) { try { t.Wait(); } catch (AggregateException ae) { ae.Handle((err) => { throw err; }); } } //------------------------------------------------- public static void RaiseUnobsrvEvtForEachIfHappens(this Task task) { task.ContinueWith(t => { var aggException = t.Exception.Flatten(); foreach (var exception in aggException.InnerExceptions) { onUnobservedTaskException(task, new EventException(exception, task)); } }, TaskContinuationOptions.OnlyOnFaulted); // not valid for multi task continuations } //------------------------------------------------- public static Task CreateHandledTask(Action action) { return CreateHandledTask(action, false); } public static Task CreateHandledTask(Action action, bool attachToParent) { Task tsk = null; tsk = CreateHandledTask(action, attachToParent, CancellationToken.None); return tsk; } public static Task CreateHandledTask(Action action, bool attachToParent, CancellationToken cancellationToken) { Task tsk = null; TaskCreationOptions atp = TaskCreationOptions.None; if (attachToParent) { atp = TaskCreationOptions.AttachedToParent; } tsk = CreateHandledTask(action, atp, cancellationToken); return tsk; } public static Task CreateHandledTask(Action action, TaskCreationOptions tco, CancellationToken cancellationToken) { Task tsk = null; tsk = Task.Factory.StartNew(action, cancellationToken, tco, TaskScheduler.Default); tsk.RaiseUnobsrvEvtForEachIfHappens(); return tsk; } public static Task CreateHandledTask(TaskScheduled mainTask, TaskScheduled onSuccessTask, TaskScheduled onCancelationTask, TaskCreationOptions tco, CancellationToken cancellationToken, params TaskScheduled[] timeouts) { Task tsk = null; ManualResetEvent me = new ManualResetEvent(false); if (timeouts == null || timeouts.Length < 1 || timeouts[0] == null) { tsk = CreateHandledTask(mainTask.action, tco, cancellationToken); me.Set(); } else { bool isCancelation = false; bool isSuccess = true; Task NonBlockCtxTask = CreateHandledTask(() => { tsk = CreateHandledTask(mainTask.action, tco, cancellationToken); me.Set(); int qtdt = timeouts.Count(st => st.action != null); CountdownEvent cde_pas = new CountdownEvent(3); CountdownEvent cde_pat = new CountdownEvent(qtdt); Parallel.ForEach<TaskScheduled>(timeouts, (ts) => { try { bool itsOnTime = tsk.Wait(ts.waitTime, cancellationToken); cde_pat.Signal(); if (!itsOnTime) { isSuccess = false; Task tact = CreateHandledTask(ts.action, TaskCreationOptions.None, cancellationToken); } } catch (OperationCanceledException oce) { isSuccess = false; cde_pat.Signal(cde_pat.CurrentCount); isCancelation = true; } }); try { isSuccess &= cde_pat.Wait(System.Threading.Timeout.Infinite, cancellationToken) && !isCancelation; } catch (OperationCanceledException oce) { isCancelation = true; isSuccess = false; } finally { cde_pas.Signal(); } try { if (isCancelation && onCancelationTask != null) { Thread.Sleep(onCancelationTask.waitTime); Task tcn = CreateHandledTask(onCancelationTask.action); } } catch { } finally { cde_pas.Signal(); } try { if (isSuccess && onSuccessTask != null) { Thread.Sleep(onSuccessTask.waitTime); Task tcn = CreateHandledTask(onSuccessTask.action); } } catch { } finally { cde_pas.Signal(); } cde_pas.Wait(System.Threading.Timeout.Infinite); }, TaskCreationOptions.None, cancellationToken); } me.WaitOne(); return tsk; } //------------------------------------------------- } |