#if NET20 || NET30 || NET35 || NET40 || !NET_4_6
using System.Security;
using System.Threading.Tasks;
namespace System.Runtime.CompilerServices
{
///
/// Provides an awaitable object that allows for configured awaits on .
///
///
///
/// This type is intended for compiler use only.
///
public struct ConfiguredTaskAwaitable
{
///
/// The underlying awaitable on whose logic this awaitable relies.
///
private readonly ConfiguredTaskAwaiter _configuredTaskAwaiter;
///
/// Initializes the .
///
/// The awaitable .true to attempt to marshal the continuation back to the original context captured; otherwise, false.
///
internal ConfiguredTaskAwaitable(Task task, bool continueOnCapturedContext)
{
_configuredTaskAwaiter = new ConfiguredTaskAwaiter(task, continueOnCapturedContext);
}
///
/// Gets an awaiter for this awaitable.
///
///
///
/// The awaiter.
///
public ConfiguredTaskAwaiter GetAwaiter()
{
return _configuredTaskAwaiter;
}
///
/// Provides an awaiter for a .
///
///
///
/// This type is intended for compiler use only.
///
public struct ConfiguredTaskAwaiter : ICriticalNotifyCompletion
{
///
/// The task being awaited.
///
private readonly Task _task;
///
/// Whether to attempt marshaling back to the original context.
///
private readonly bool _continueOnCapturedContext;
///
/// Gets whether the task being awaited is completed.
///
///
///
/// This property is intended for compiler user rather than use directly in code.
///
/// The awaiter was not properly initialized.
public bool IsCompleted
{
get { return _task.IsCompleted; }
}
///
/// Initializes the .
///
/// The awaitable .true to attempt to marshal the continuation back to the original context captured; otherwise, false.
///
internal ConfiguredTaskAwaiter(Task task, bool continueOnCapturedContext)
{
_task = task;
_continueOnCapturedContext = continueOnCapturedContext;
}
///
/// Schedules the continuation onto the associated with this .
///
/// The action to invoke when the await operation completes.The argument is null (Nothing in Visual Basic).The awaiter was not properly initialized.
///
/// This method is intended for compiler user rather than use directly in code.
///
public void OnCompleted(Action continuation)
{
TaskAwaiter.OnCompletedInternal(_task, continuation, _continueOnCapturedContext);
}
///
/// Schedules the continuation onto the associated with this .
///
/// The action to invoke when the await operation completes.The argument is null (Nothing in Visual Basic).The awaiter was not properly initialized.
///
/// This method is intended for compiler user rather than use directly in code.
///
[SecurityCritical]
public void UnsafeOnCompleted(Action continuation)
{
TaskAwaiter.OnCompletedInternal(_task, continuation, true);
}
///
/// Ends the await on the completed .
///
///
///
/// The result of the completed .
///
/// The awaiter was not properly initialized.The task was not yet completed.The task was canceled.The task completed in a Faulted state.
public TResult GetResult()
{
TaskAwaiter.ValidateEnd(_task);
return _task.Result;
}
}
}
///
/// Provides an awaitable object that allows for configured awaits on .
///
///
///
/// This type is intended for compiler use only.
///
public struct ConfiguredTaskAwaitable
{
///
/// The task being awaited.
///
private readonly ConfiguredTaskAwaiter _configuredTaskAwaiter;
///
/// Initializes the .
///
/// The awaitable .true to attempt to marshal the continuation back to the original context captured; otherwise, false.
///
internal ConfiguredTaskAwaitable(Task task, bool continueOnCapturedContext)
{
_configuredTaskAwaiter = new ConfiguredTaskAwaiter(task, continueOnCapturedContext);
}
///
/// Gets an awaiter for this awaitable.
///
///
///
/// The awaiter.
///
public ConfiguredTaskAwaiter GetAwaiter()
{
return _configuredTaskAwaiter;
}
///
/// Provides an awaiter for a .
///
///
///
/// This type is intended for compiler use only.
///
public struct ConfiguredTaskAwaiter : ICriticalNotifyCompletion
{
///
/// The task being awaited.
///
private readonly Task _task;
///
/// Whether to attempt marshaling back to the original context.
///
private readonly bool _continueOnCapturedContext;
///
/// Gets whether the task being awaited is completed.
///
///
///
/// This property is intended for compiler user rather than use directly in code.
///
/// The awaiter was not properly initialized.
public bool IsCompleted
{
get { return _task.IsCompleted; }
}
///
/// Initializes the .
///
/// The to await.true to attempt to marshal the continuation back to the original context captured
/// when BeginAwait is called; otherwise, false.
///
internal ConfiguredTaskAwaiter(Task task, bool continueOnCapturedContext)
{
_task = task;
_continueOnCapturedContext = continueOnCapturedContext;
}
///
/// Schedules the continuation onto the associated with this .
///
/// The action to invoke when the await operation completes.
/// The argument is null (Nothing in Visual Basic).
/// The awaiter was not properly initialized.
///
/// This method is intended for compiler user rather than use directly in code.
///
public void OnCompleted(Action continuation)
{
TaskAwaiter.OnCompletedInternal(_task, continuation, _continueOnCapturedContext);
}
///
/// Schedules the continuation onto the associated with this .
///
/// The action to invoke when the await operation completes.
/// The argument is null (Nothing in Visual Basic).
/// The awaiter was not properly initialized.
///
/// This method is intended for compiler user rather than use directly in code.
///
[SecurityCritical]
public void UnsafeOnCompleted(Action continuation)
{
TaskAwaiter.OnCompletedInternal(_task, continuation, true);
}
///
/// Ends the await on the completed .
///
///
///
/// The result of the completed .
///
/// The awaiter was not properly initialized.The task was not yet completed.The task was canceled.The task completed in a Faulted state.
public void GetResult()
{
TaskAwaiter.ValidateEnd(_task);
}
}
}
}
#endif