using System; using System.Threading; using JetBrains.Annotations; namespace Abp { /// /// This class can be used to provide an action when /// Dipose method is called. /// public class DisposeAction : IDisposable { public static readonly DisposeAction Empty = new DisposeAction(null); private Action _action; /// /// Creates a new object. /// /// Action to be executed when this object is disposed. public DisposeAction([CanBeNull] Action action) { _action = action; } public void Dispose() { // Interlocked prevents multiple execution of the _action. var action = Interlocked.Exchange(ref _action, null); action?.Invoke(); } } }