Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Concurrency/SynchronizationContextScheduler.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.Disposables;
5 using System.Threading;
6
7 namespace System.Reactive.Concurrency
8 {
9 /// <summary>
10 /// Represents an object that schedules units of work on a provided <seealso cref="T:System.Threading.SynchronizationContext"/>.
11 /// </summary>
12 public class SynchronizationContextScheduler : LocalScheduler
13 {
14 private readonly SynchronizationContext _context;
15 private readonly bool _alwaysPost;
16
17 /// <summary>
18 /// Creates an object that schedules units of work on the provided <see cref="T:System.Threading.SynchronizationContext"/>.
19 /// </summary>
20 /// <param name="context">Synchronization context to schedule units of work on.</param>
21 /// <exception cref="ArgumentNullException"><paramref name="context"/> is null.</exception>
22 public SynchronizationContextScheduler(SynchronizationContext context)
23 {
24 if (context == null)
25 throw new ArgumentNullException("context");
26
27 _context = context;
28 _alwaysPost = true;
29 }
30
31 /// <summary>
32 /// Creates an object that schedules units of work on the provided <see cref="T:System.Threading.SynchronizationContext"/>.
33 /// </summary>
34 /// <param name="context">Synchronization context to schedule units of work on.</param>
35 /// <param name="alwaysPost">Configures whether scheduling always posts to the synchronization context, regardless whether the caller is on the same synchronization context.</param>
36 /// <exception cref="ArgumentNullException"><paramref name="context"/> is null.</exception>
37 public SynchronizationContextScheduler(SynchronizationContext context, bool alwaysPost)
38 {
39 if (context == null)
40 throw new ArgumentNullException("context");
41
42 _context = context;
43 _alwaysPost = alwaysPost;
44 }
45
46 /// <summary>
47 /// Schedules an action to be executed.
48 /// </summary>
49 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
50 /// <param name="state">State passed to the action to be executed.</param>
51 /// <param name="action">Action to be executed.</param>
52 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
53 /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
54 public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
55 {
56 if (action == null)
57 throw new ArgumentNullException("action");
58
59 var d = new SingleAssignmentDisposable();
60
61 if (!_alwaysPost && _context == SynchronizationContext.Current)
62 {
63 d.Disposable = action(this, state);
64 }
65 else
66 {
67 _context.PostWithStartComplete(() =>
68 {
69 if (!d.IsDisposed)
70 d.Disposable = action(this, state);
71 });
72 }
73
74 return d;
75 }
76
77 /// <summary>
78 /// Schedules an action to be executed after dueTime.
79 /// </summary>
80 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
81 /// <param name="state">State passed to the action to be executed.</param>
82 /// <param name="action">Action to be executed.</param>
83 /// <param name="dueTime">Relative time after which to execute the action.</param>
84 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
85 /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
86 public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
87 {
88 if (action == null)
89 throw new ArgumentNullException("action");
90
91 var dt = Scheduler.Normalize(dueTime);
92 if (dt.Ticks == 0)
93 return Schedule(state, action);
94
95 return DefaultScheduler.Instance.Schedule(state, dt, (_, state1) => Schedule(state1, action));
96 }
97 }
98 }
99 #endif