Merge pull request #3386 from alexanderkyte/nunit_lite_return_status
[mono.git] / mcs / class / referencesource / System.Web / Util / TaskExtensions.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="TaskExtensions.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 namespace System.Web.Util {
8     using System;
9     using System.Threading.Tasks;
10
11     // Contains helper methods for dealing with Tasks
12
13     internal static class TaskExtensions {
14
15         // Throws the exception that faulted a Task, similar to what 'await' would have done.
16         // Useful for synchronous methods which have a Task instance they know to be already completed
17         // and where they want to let the exception propagate upward.
18         public static void ThrowIfFaulted(this Task task) {
19             Debug.Assert(task.IsCompleted, "The Task passed to this method must be marked as completed so that this method doesn't block.");
20             task.GetAwaiter().GetResult();
21         }
22
23         // Gets a WithinCancellableCallbackTaskAwaiter from a Task.
24         public static WithinCancellableCallbackTaskAwaitable WithinCancellableCallback(this Task task, HttpContext context) {
25             return new WithinCancellableCallbackTaskAwaitable(context, task.GetAwaiter());
26         }
27
28     }
29 }