Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / System.Threading.Tasks / Task.cs
index 8d75e83951681a17270a77139a161ed1f349233a..bb023257b4769978f0db08de9d5a5a4a03119811 100644 (file)
@@ -186,7 +186,7 @@ namespace System.Threading.Tasks
                                throw new InvalidOperationException ("Start may not be called on a promise-style task");
 
                        SetupScheduler (scheduler);
-                       Schedule ();
+                       Schedule (true);
                }
 
                internal void SetupScheduler (TaskScheduler scheduler)
@@ -214,10 +214,10 @@ namespace System.Threading.Tasks
                        if (IsPromise)
                                throw new InvalidOperationException ("RunSynchronously may not be called on a promise-style task");
 
-                       RunSynchronouslyCore (scheduler);
+                       RunSynchronouslyCore (scheduler, true);
                }
 
-               internal void RunSynchronouslyCore (TaskScheduler scheduler)
+               internal void RunSynchronouslyCore (TaskScheduler scheduler, bool throwException)
                {
                        SetupScheduler (scheduler);
                        Status = TaskStatus.WaitingToRun;
@@ -228,10 +228,13 @@ namespace System.Threading.Tasks
                        } catch (Exception inner) {
                                var ex = new TaskSchedulerException (inner);
                                TrySetException (new AggregateException (ex), false, true);
-                               throw ex;
+                               if (throwException)
+                                       throw ex;
+
+                               return;
                        }
 
-                       Schedule ();
+                       Schedule (throwException);
                        WaitCore (Timeout.Infinite, CancellationToken.None, false);
                }
                #endregion
@@ -381,10 +384,17 @@ namespace System.Threading.Tasks
                #endregion
                
                #region Internal and protected thingies
-               internal void Schedule ()
+               internal void Schedule (bool throwException)
                {
                        Status = TaskStatus.WaitingToRun;
-                       scheduler.QueueTask (this);
+                       try {
+                               scheduler.QueueTask (this);
+                       } catch (Exception inner) {
+                               var ex = new TaskSchedulerException (inner);
+                               TrySetException (new AggregateException (ex), false, true);
+                               if (throwException)
+                                       throw ex;
+                       }
                }
                
                void ThreadStart ()
@@ -687,8 +697,13 @@ namespace System.Threading.Tasks
                                return true;
 
                        // If the task is ready to be run and we were supposed to wait on it indefinitely without cancellation, just run it
-                       if (runInline && Status == TaskStatus.WaitingToRun && millisecondsTimeout == Timeout.Infinite && scheduler != null && !cancellationToken.CanBeCanceled)
-                               scheduler.RunInline (this, true);
+                       if (runInline && Status == TaskStatus.WaitingToRun && millisecondsTimeout == Timeout.Infinite && scheduler != null && !cancellationToken.CanBeCanceled) {
+                               try {
+                                       scheduler.RunInline (this, true);
+                               } catch (Exception e) {
+                                       throw new TaskSchedulerException (e);
+                               }
+                       }
 
                        bool result = true;