summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/TaskExtensions.cs
blob: 51f275d7347a0947bd8058b1885fe8b98b0fb3f1 (plain)
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
using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;

namespace Xamarin.Forms
{
	internal static class TaskExtensions
	{
		public static void WatchForError(this IAsyncAction self)
		{
			self.AsTask().WatchForError();
		}

		public static void WatchForError<T>(this IAsyncOperation<T> self)
		{
			self.AsTask().WatchForError();
		}

		public static void WatchForError(this Task self)
		{
			SynchronizationContext context = SynchronizationContext.Current;
			if (context == null)
				return;

			self.ContinueWith(t => {
				                       Exception exception = t.Exception.InnerExceptions.Count > 1 ? t.Exception : t.Exception.InnerException;

				                       context.Post(e => { throw (Exception)e; }, exception);
			}, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default);
		}
	}
}