This repository has been archived by the owner on Nov 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
TaskWrapperAsyncResult.cs
71 lines (62 loc) · 2.85 KB
/
TaskWrapperAsyncResult.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Threading;
using System.Threading.Tasks;
namespace System.Web.Http.WebHost
{
/// <summary>
/// Wraps a <see cref="Task"/>, optionally overriding the State object (since the Task Asynchronous Pattern doesn't normally use it).
/// </summary>
/// <remarks>Class copied from System.Web.Mvc, but with modifications</remarks>
internal sealed class TaskWrapperAsyncResult : IAsyncResult
{
private bool? _completedSynchronously;
/// <summary>
/// Initializes a new instance of the <see cref="TaskWrapperAsyncResult"/> class.
/// </summary>
/// <param name="task">The <see cref="Task"/> to wrap.</param>
/// <param name="asyncState">User-defined object that qualifies or contains information about an asynchronous operation.</param>
public TaskWrapperAsyncResult(Task task, object asyncState)
{
if (task == null)
{
throw Error.ArgumentNull("task");
}
Task = task;
AsyncState = asyncState;
}
/// <summary>
/// Gets a user-defined object that qualifies or contains information about an asynchronous operation.
/// </summary>
/// <returns>A user-defined object that qualifies or contains information about an asynchronous operation.</returns>
public object AsyncState { get; private set; }
/// <summary>
/// Gets a <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.
/// </summary>
/// <returns>A <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.</returns>
public WaitHandle AsyncWaitHandle
{
get { return ((IAsyncResult)Task).AsyncWaitHandle; }
}
/// <summary>
/// Gets a value indicating whether the asynchronous operation completed synchronously.
/// </summary>
/// <returns>true if the asynchronous operation completed synchronously; otherwise, false.</returns>
public bool CompletedSynchronously
{
get { return _completedSynchronously ?? ((IAsyncResult)Task).CompletedSynchronously; }
internal set { _completedSynchronously = value; }
}
/// <summary>
/// Gets a value indicating whether the asynchronous operation has completed.
/// </summary>
/// <returns>true if the operation is complete; otherwise, false.</returns>
public bool IsCompleted
{
get { return ((IAsyncResult)Task).IsCompleted; }
}
/// <summary>
/// Gets the task.
/// </summary>
public Task Task { get; private set; }
}
}