Debian Codegraph @

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

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !NO_SYNCCTX
4 using System.Reactive.Concurrency;
5 using System.Threading;
6
7 namespace System.Reactive.Disposables
8 {
9 /// <summary>
10 /// Represents a disposable resource whose disposal invocation will be posted to the specified <seealso cref="T:System.Threading.SynchronizationContext"/>.
11 /// </summary>
12 public sealed class ContextDisposable : ICancelable
13 {
14 private readonly SynchronizationContext _context;
15 private volatile IDisposable _disposable;
16
17 /// <summary>
18 /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.ContextDisposable"/> class that uses the specified <see cref="T:System.Threading.SynchronizationContext"/> on which to dispose the specified disposable resource.
19 /// </summary>
20 /// <param name="context">Context to perform disposal on.</param>
21 /// <param name="disposable">Disposable whose Dispose operation to run on the given synchronization context.</param>
22 /// <exception cref="ArgumentNullException"><paramref name="context"/> or <paramref name="disposable"/> is null.</exception>
23 public ContextDisposable(SynchronizationContext context, IDisposable disposable)
24 {
25 if (context == null)
26 throw new ArgumentNullException("context");
27 if (disposable == null)
28 throw new ArgumentNullException("disposable");
29
30 _context = context;
31 _disposable = disposable;
32 }
33
34 /// <summary>
35 /// Gets the provided <see cref="T:System.Threading.SynchronizationContext"/>.
36 /// </summary>
37 public SynchronizationContext Context
38 {
39 get { return _context; }
40 }
41
42 /// <summary>
43 /// Gets a value that indicates whether the object is disposed.
44 /// </summary>
45 public bool IsDisposed
46 {
47 get { return _disposable == BooleanDisposable.True; }
48 }
49
50 /// <summary>
51 /// Disposes the underlying disposable on the provided <see cref="T:System.Threading.SynchronizationContext"/>.
52 /// </summary>
53 public void Dispose()
54 {
55 #pragma warning disable 0420
56 var disposable = Interlocked.Exchange(ref _disposable, BooleanDisposable.True);
57 #pragma warning restore 0420
58
59 if (disposable != BooleanDisposable.True)
60 {
61 _context.PostWithStartComplete(d => d.Dispose(), disposable);
62 }
63 }
64 }
65 }
66 #endif