Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Disposables/AnonymousDisposable.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System.Threading;
4
5 namespace System.Reactive.Disposables
6 {
7 /// <summary>
8 /// Represents an Action-based disposable.
9 /// </summary>
10 internal sealed class AnonymousDisposable : ICancelable
11 {
12 private volatile Action _dispose;
13
14 /// <summary>
15 /// Constructs a new disposable with the given action used for disposal.
16 /// </summary>
17 /// <param name="dispose">Disposal action which will be run upon calling Dispose.</param>
18 public AnonymousDisposable(Action dispose)
19 {
20 System.Diagnostics.Debug.Assert(dispose != null);
21
22 _dispose = dispose;
23 }
24
25 /// <summary>
26 /// Gets a value that indicates whether the object is disposed.
27 /// </summary>
28 public bool IsDisposed
29 {
30 get { return _dispose == null; }
31 }
32
33 /// <summary>
34 /// Calls the disposal action if and only if the current instance hasn't been disposed yet.
35 /// </summary>
36 public void Dispose()
37 {
38 #pragma warning disable 0420
39 var dispose = Interlocked.Exchange(ref _dispose, null);
40 #pragma warning restore 0420
41 if (dispose != null)
42 {
43 dispose();
44 }
45 }
46 }
47 }