// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace EpicGames.Core
{
///
/// Wrapper class to create a waitable task out of a cancellation token
///
public sealed class CancellationTask : IDisposable
{
///
/// Completion source for the task
///
readonly TaskCompletionSource _completionSource;
///
/// Registration handle with the cancellation token
///
readonly CancellationTokenRegistration _registration;
///
/// Constructor
///
/// The cancellation token to register with
public CancellationTask(CancellationToken token)
{
_completionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
_registration = token.Register(() => _completionSource.TrySetResult(true));
}
///
/// The task that can be waited on
///
public Task Task => _completionSource.Task;
///
/// Dispose of any allocated resources
///
public void Dispose()
{
_registration.Dispose();
}
}
}