// Copyright (c) Microsoft Corporation. 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
{
///
/// Wraps a , optionally overriding the State object (since the Task Asynchronous Pattern doesn't normally use it).
///
/// Class copied from System.Web.Mvc, but with modifications
internal sealed class TaskWrapperAsyncResult : IAsyncResult
{
private bool? _completedSynchronously;
///
/// Initializes a new instance of the class.
///
/// The to wrap.
/// User-defined object that qualifies or contains information about an asynchronous operation.
public TaskWrapperAsyncResult(Task task, object asyncState)
{
if (task == null)
{
throw Error.ArgumentNull("task");
}
Task = task;
AsyncState = asyncState;
}
///
/// Gets a user-defined object that qualifies or contains information about an asynchronous operation.
///
/// A user-defined object that qualifies or contains information about an asynchronous operation.
public object AsyncState { get; private set; }
///
/// Gets a that is used to wait for an asynchronous operation to complete.
///
/// A that is used to wait for an asynchronous operation to complete.
public WaitHandle AsyncWaitHandle
{
get { return ((IAsyncResult)Task).AsyncWaitHandle; }
}
///
/// Gets a value indicating whether the asynchronous operation completed synchronously.
///
/// true if the asynchronous operation completed synchronously; otherwise, false.
public bool CompletedSynchronously
{
get { return _completedSynchronously ?? ((IAsyncResult)Task).CompletedSynchronously; }
internal set { _completedSynchronously = value; }
}
///
/// Gets a value indicating whether the asynchronous operation has completed.
///
/// true if the operation is complete; otherwise, false.
public bool IsCompleted
{
get { return ((IAsyncResult)Task).IsCompleted; }
}
///
/// Gets the task.
///
public Task Task { get; private set; }
}
}