Note that there are some explanatory texts on larger screens.

plurals
  1. POUnobservedTaskException being throw but it is handled by a TaskScheduler.UnobservedTaskException handler and a continuations OnlyOnFaulted handler
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/2707295/how-to-handle-all-unhandled-exceptions-when-using-task-parallel-library">How to handle all unhandled exceptions when using Task Parallel Library?</a> </p> </blockquote> <hr> <p>@Buu Nguyen<br> Hi, solution is based on your idea </p> <h2>Someone put it as possible duplicate, there is something different here. I told about that answer and the idea helped me here. There is a comment of mine.</h2> <p>I having problems with TPL programming.<br> Im getting UnobservedTaskException, im using @h4165f8ghd4f854d6f8h solution on <strong>[</strong> <a href="https://stackoverflow.com/questions/7883052/a-tasks-exceptions-were-not-observed-either-by-waiting-on-the-task-or-accessi/11830087#11830087">A Task&#39;s exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was</a> <strong>]</strong> to handle exceptions but still getting UnobservedTaskException.<br> I put the following code before start tasks too: </p> <pre><code>TaskScheduler.UnobservedTaskException += (sender, e) =&gt; { e.SetObserved(); throw e.Exception; }; </code></pre> <p>but [ <a href="https://stackoverflow.com/questions/10874068/exception-thrown-in-task-thread-not-caught-by-unobservedtaskexception">Exception thrown in Task Thread, not caught by UnobservedTaskException</a> ] telling it dont catch every TPL unhandled exception. </p> <p>I want bubble exceptions until reach top of stack then deal with it.<br> Someone can help me ????</p> <hr> <p>@Jon Skeet </p> <p>Hi, i did it smaller possible<br> Its edited now<br> thanks for see </p> <hr> <p>Did something more</p> <pre><code>class Program { static void Main(string[] args) { Program p = new Program(); p.tplTestOne(); } public void tplTestOne() { //------------------------------------------------- MyClassHere.onUnobservedTaskException += (object sender, EventException e) =&gt; { Console.WriteLine(e.Exception.Message); //its fired OK }; TaskScheduler.UnobservedTaskException += (object sender, UnobservedTaskExceptionEventArgs e) =&gt; { Console.WriteLine(e.Exception.Message); // its not fired, buggy }; //------------------------------------------------- CancellationTokenSource source = new CancellationTokenSource(); Task tz = MyClassHere.CreateHandledTask( new TaskScheduled(0, () =&gt; { if (!source.IsCancellationRequested) { Console.WriteLine("A-main-task-started"); } Thread.Sleep(5000); if (source.IsCancellationRequested) { Console.WriteLine("CancelingMainTask"); } }) , new TaskScheduled(3000, () =&gt; { Console.WriteLine("okTaskCalled"); }) , null //new TaskScheduled(0, () =&gt; { Console.WriteLine("cancelTaskCalled"); }) , TaskCreationOptions.AttachedToParent , source.Token , new TaskScheduled(2000, () =&gt; { if (!source.IsCancellationRequested) { Console.WriteLine("B-timeout"); } }) , new TaskScheduled(1000, () =&gt; { if (!source.IsCancellationRequested) { Console.WriteLine("C-timeout"); } source.Cancel(); }) ); if(tz != null) { tz.ContinueWith(t =&gt; { Console.WriteLine("END"); }); } Task tsk_1 = MyClassHere.createHandledTask(() =&gt; { double x = 1; x = (x + 1) / x; }, false); Task tsk_2 = MyClassHere.createHandledTask(() =&gt; { double y = 0; throw new Exception("forced_divisionbyzerodontthrowanymore_test"); // here -&gt; System.Exception was unhandled by user code }, true); Task tsk_3 = MyClassHere.createHandledTask(() =&gt; { double z = 1; z = (z + 1) / z; }, true); Task tsk_4 = MyClassHere.createHandledTask(() =&gt; { 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) =&gt; { throw err; }); } } //------------------------------------------------- public static void RaiseUnobsrvEvtForEachIfHappens(this Task task) { task.ContinueWith(t =&gt; { 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 &lt; 1 || timeouts[0] == null) { tsk = CreateHandledTask(mainTask.action, tco, cancellationToken); me.Set(); } else { bool isCancelation = false; bool isSuccess = true; Task NonBlockCtxTask = CreateHandledTask(() =&gt; { tsk = CreateHandledTask(mainTask.action, tco, cancellationToken); me.Set(); int qtdt = timeouts.Count(st =&gt; st.action != null); CountdownEvent cde_pas = new CountdownEvent(3); CountdownEvent cde_pat = new CountdownEvent(qtdt); Parallel.ForEach&lt;TaskScheduled&gt;(timeouts, (ts) =&gt; { 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 &amp;= cde_pat.Wait(System.Threading.Timeout.Infinite, cancellationToken) &amp;&amp; !isCancelation; } catch (OperationCanceledException oce) { isCancelation = true; isSuccess = false; } finally { cde_pas.Signal(); } try { if (isCancelation &amp;&amp; onCancelationTask != null) { Thread.Sleep(onCancelationTask.waitTime); Task tcn = CreateHandledTask(onCancelationTask.action); } } catch { } finally { cde_pas.Signal(); } try { if (isSuccess &amp;&amp; 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; } //------------------------------------------------- } </code></pre> <p>Thanks </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload