You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readarr/src/Microsoft.AspNet.SignalR.Core/Infrastructure/DisposableAction.cs

44 lines
1.2 KiB

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
using System.Threading;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.AspNet.SignalR.Infrastructure
{
internal class DisposableAction : IDisposable
{
[SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "The client projects use this.")]
public static readonly DisposableAction Empty = new DisposableAction(() => { });
private Action<object> _action;
private readonly object _state;
public DisposableAction(Action action)
: this(state => ((Action)state).Invoke(), state: action)
{
}
public DisposableAction(Action<object> action, object state)
{
_action = action;
_state = state;
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Interlocked.Exchange(ref _action, (state) => { }).Invoke(_state);
}
}
public void Dispose()
{
Dispose(true);
}
}
}