// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.ComponentModel; using System.Threading; using System.Threading.Tasks; #pragma warning disable CA1063 namespace CommunityToolkit.Common.Deferred; /// /// Deferral handle provided by a . /// public class EventDeferral : IDisposable { //// TODO: If/when .NET 6 is base, we can upgrade to non-generic version private readonly TaskCompletionSource _taskCompletionSource = new(); internal EventDeferral() { } /// /// Call when finished with the Deferral. /// public void Complete() => _taskCompletionSource.TrySetResult(null); /// /// Waits for the to be completed by the event handler. /// /// . /// . [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This is an internal only method to be used by EventHandler extension classes, public callers should call GetDeferral() instead on the DeferredEventArgs.")] public async Task WaitForCompletion(CancellationToken cancellationToken) { using (cancellationToken.Register(() => _taskCompletionSource.TrySetCanceled())) { _ = await _taskCompletionSource.Task; } } /// public void Dispose() { Complete(); } }