Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to propagate thread exceptions back to application exception handler

$
0
0

Please see my question on Stackoverflow here:

http://stackoverflow.com/questions/17411297/how-to-propagate-thread-exceptions-back-to-application-exception-handler

As stated in the above link my objective is to handle all exceptions on all threads in a global exception handler.  After posting on SO I found this document which is very helpful:

http://msdn.microsoft.com/en-us/library/dd997415(v=vs.100).aspx

I tried to modify the code in the link above to fit my purpose.  I added a continuation which causes it to crash with the exception noted in the code below.  Can you please assist me in accomplishing the objective above?

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Task task = Task.Factory.StartNew(() =>
            {
                Task t2 = Task.Factory.StartNew(() =>
                {
                    throw new Exception("boo");  // The current SynchronizationContext may not be used as a TaskScheduler.
                }, CancellationToken.None,TaskCreationOptions.None,TaskScheduler.FromCurrentSynchronizationContext())
                .ContinueWith(t => 
                { 
                    int z = 1; 
                }, CancellationToken.None,TaskContinuationOptions.NotOnFaulted,TaskScheduler.FromCurrentSynchronizationContext());
                t2.Wait();
            }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
            try
            {
                task.Wait();
            }
            catch (Exception ex)
            {
                // The app exception handler should catch this:
                throw new Exception("Task", ex);
            }
        }
    }


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>