forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskHelpersExtensions.cs
More file actions
55 lines (49 loc) · 2 KB
/
TaskHelpersExtensions.cs
File metadata and controls
55 lines (49 loc) · 2 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics.CodeAnalysis;
namespace System.Threading.Tasks
{
internal static class TaskHelpersExtensions
{
/// <summary>
/// Cast Task to Task of object
/// </summary>
internal static async Task<object> CastToObject(this Task task)
{
await task;
return null;
}
/// <summary>
/// Cast Task of T to Task of object
/// </summary>
internal static async Task<object> CastToObject<T>(this Task<T> task)
{
return (object)await task;
}
/// <summary>
/// Throws the first faulting exception for a task which is faulted. It preserves the original stack trace when
/// throwing the exception. Note: It is the caller's responsibility not to pass incomplete tasks to this
/// method, because it does degenerate into a call to the equivalent of .Wait() on the task when it hasn't yet
/// completed.
/// </summary>
internal static void ThrowIfFaulted(this Task task)
{
task.GetAwaiter().GetResult();
}
/// <summary>
/// Attempts to get the result value for the given task. If the task ran to completion, then
/// it will return true and set the result value; otherwise, it will return false.
/// </summary>
[SuppressMessage("Microsoft.Web.FxCop", "MW1201", Justification = "The usages here are deemed safe, and provide the implementations that this rule relies upon.")]
internal static bool TryGetResult<TResult>(this Task<TResult> task, out TResult result)
{
if (task.Status == TaskStatus.RanToCompletion)
{
result = task.Result;
return true;
}
result = default(TResult);
return false;
}
}
}