Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:25:04+00:00 2026-06-09T14:25:04+00:00

I’m having problems with TPL programming. I’m getting UnobservedTaskException while using @h4165f8ghd4f854d6f8h solution on

  • 0

I’m having problems with TPL programming.
I’m getting UnobservedTaskException while using @h4165f8ghd4f854d6f8h solution on [ http://stackoverflow.com/questions/7883052/a-tasks-exceptions-were-not-observed-either-by-waiting-on-the-task-or-accessi/11830087#11830087 ] to handle exceptions but still getting UnobservedTaskException.
I added the following code before starting tasks too:

    TaskScheduler.UnobservedTaskException += (sender, e) =>
    {
        e.SetObserved();
        throw e.Exception;
    };  

but [ http://stackoverflow.com/questions/10874068/exception-thrown-in-task-thread-not-caught-by-unobservedtaskexception ] telling it won’t catch every TPL unhandled exception.

I want propagate exceptions until reach top of stack then deal with it.
Can someone help me????


@Jon Skeet

Hi, I did a smaller repro, thanks for checking

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;
    }

}
    

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:25:06+00:00Added an answer on June 9, 2026 at 2:25 pm

    The solution was based on How to handle all unhandled exceptions when using Task Parallel Library?

    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;
        }
        //-------------------------------------------------
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.