1
0
mirror of https://github.com/chylex/.NET-Community-Toolkit.git synced 2025-06-02 05:34:09 +02:00
.NET-Community-Toolkit/CommunityToolkit.Common/Deferred/DeferredEventArgs.cs
Sergio Pedri 51c7a67c20 More code style improvements and tweaks
- Remove unused namespaces
- Remove unused attributes
- Remove unnecessary #nullable enable directives
- Sort using directives
- Simplify names
2021-11-01 22:29:46 +01:00

60 lines
2.0 KiB
C#

// 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;
#pragma warning disable CA1001
namespace CommunityToolkit.Common.Deferred;
/// <summary>
/// <see cref="EventArgs"/> which can retrieve a <see cref="EventDeferral"/> in order to process data asynchronously before an <see cref="EventHandler"/> completes and returns to the calling control.
/// </summary>
public class DeferredEventArgs : EventArgs
{
/// <summary>
/// Gets a new <see cref="DeferredEventArgs"/> to use in cases where no <see cref="EventArgs"/> wish to be provided.
/// </summary>
public static new DeferredEventArgs Empty => new();
private readonly object _eventDeferralLock = new();
private EventDeferral? _eventDeferral;
/// <summary>
/// Returns an <see cref="EventDeferral"/> which can be completed when deferred event is ready to continue.
/// </summary>
/// <returns><see cref="EventDeferral"/> instance.</returns>
public EventDeferral GetDeferral()
{
lock (_eventDeferralLock)
{
return _eventDeferral ??= new EventDeferral();
}
}
/// <summary>
/// DO NOT USE - This is a support method used by <see cref="EventHandlerExtensions"/>. It is public only for
/// additional usage within extensions for the UWP based TypedEventHandler extensions.
/// </summary>
/// <returns>Internal EventDeferral reference</returns>
#if !NETSTANDARD1_4
[Browsable(false)]
#endif
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This is an internal only method to be used by EventHandler extension classes, public callers should call GetDeferral() instead.")]
public EventDeferral? GetCurrentDeferralAndReset()
{
lock (_eventDeferralLock)
{
EventDeferral? eventDeferral = _eventDeferral;
_eventDeferral = null;
return eventDeferral;
}
}
}