summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT/TaskExtensions.cs
blob: 332e8a64294fb361a2b1b8d39c835b7995a29eb1 (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
34
35
36
37
38
39
40
using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;

#if WINDOWS_UWP

namespace Xamarin.Forms.Platform.UWP
#else

namespace Xamarin.Forms.Platform.WinRT
#endif
{
	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);
		}
	}
}