Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Windows.Threading/Reactive/Concurrency/DispatcherScheduler.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 !WINDOWS
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 <see cref="System.Windows.Threading.Dispatcher"/>.
11 /// </summary>
12 /// <remarks>
13 /// This scheduler type is typically used indirectly through the <see cref="System.Reactive.Linq.DispatcherObservable.ObserveOnDispatcher&lt;TSource&gt;(IObservable&lt;TSource&gt;)"/> and <see cref="System.Reactive.Linq.DispatcherObservable.SubscribeOnDispatcher&lt;TSource&gt;(IObservable&lt;TSource&gt;)"/> methods that use the Dispatcher on the calling thread.
14 /// </remarks>
15 public class DispatcherScheduler : LocalScheduler, ISchedulerPeriodic
16 {
17 /// <summary>
18 /// Gets the scheduler that schedules work on the current <see cref="System.Windows.Threading.Dispatcher"/>.
19 /// </summary>
20 [Obsolete(Constants_WindowsThreading.OBSOLETE_INSTANCE_PROPERTY)]
21 public static DispatcherScheduler Instance
22 {
23 get
24 {
25 return new DispatcherScheduler(
26 #if USE_SL_DISPATCHER
27 System.Windows.Deployment.Current.Dispatcher
28 #else
29 System.Windows.Threading.Dispatcher.CurrentDispatcher
30 #endif
31 );
32 }
33 }
34
35 /// <summary>
36 /// Gets the scheduler that schedules work on the <see cref="System.Windows.Threading.Dispatcher"/> for the current thread.
37 /// </summary>
38 public static DispatcherScheduler Current
39 {
40 get
41 {
42 #if USE_SL_DISPATCHER
43 return new DispatcherScheduler(System.Windows.Deployment.Current.Dispatcher);
44 #else
45 var dispatcher = System.Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread);
46 if (dispatcher == null)
47 throw new InvalidOperationException(Strings_WindowsThreading.NO_DISPATCHER_CURRENT_THREAD);
48
49 return new DispatcherScheduler(dispatcher);
50 #endif
51 }
52 }
53
54 System.Windows.Threading.Dispatcher _dispatcher;
55
56 #if HAS_DISPATCHER_PRIORITY
57 System.Windows.Threading.DispatcherPriority _priority;
58 #endif
59
60 /// <summary>
61 /// Constructs a DispatcherScheduler that schedules units of work on the given <see cref="System.Windows.Threading.Dispatcher"/>.
62 /// </summary>
63 /// <param name="dispatcher">Dispatcher to schedule work on.</param>
64 /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is null.</exception>
65 public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher)
66 {
67 if (dispatcher == null)
68 throw new ArgumentNullException("dispatcher");
69
70 _dispatcher = dispatcher;
71 #if HAS_DISPATCHER_PRIORITY
72 _priority = Windows.Threading.DispatcherPriority.Normal;
73 #endif
74 }
75
76 #if HAS_DISPATCHER_PRIORITY
77 /// <summary>
78 /// Constructs a DispatcherScheduler that schedules units of work on the given <see cref="System.Windows.Threading.Dispatcher"/> at the given priority.
79 /// </summary>
80 /// <param name="dispatcher">Dispatcher to schedule work on.</param>
81 /// <param name="priority">Priority at which units of work are scheduled.</param>
82 /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is null.</exception>
83 public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher, System.Windows.Threading.DispatcherPriority priority)
84 {
85 if (dispatcher == null)
86 throw new ArgumentNullException("dispatcher");
87
88 _dispatcher = dispatcher;
89 _priority = priority;
90 }
91 #endif
92
93 /// <summary>
94 /// Gets the <see cref="System.Windows.Threading.Dispatcher"/> associated with the DispatcherScheduler.
95 /// </summary>
96 public System.Windows.Threading.Dispatcher Dispatcher
97 {
98 get { return _dispatcher; }
99 }
100
101 #if HAS_DISPATCHER_PRIORITY
102 /// <summary>
103 /// Gets the priority at which work items will be dispatched.
104 /// </summary>
105 public System.Windows.Threading.DispatcherPriority Priority
106 {
107 get { return _priority; }
108 }
109 #endif
110
111 /// <summary>
112 /// Schedules an action to be executed on the dispatcher.
113 /// </summary>
114 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
115 /// <param name="state">State passed to the action to be executed.</param>
116 /// <param name="action">Action to be executed.</param>
117 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
118 /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
119 public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
120 {
121 if (action == null)
122 throw new ArgumentNullException("action");
123
124 var d = new SingleAssignmentDisposable();
125
126 _dispatcher.BeginInvoke(
127 new Action(() =>
128 {
129 if (!d.IsDisposed)
130 d.Disposable = action(this, state);
131 })
132 #if HAS_DISPATCHER_PRIORITY
133 , _priority
134 #endif
135 );
136
137 return d;
138 }
139
140 /// <summary>
141 /// Schedules an action to be executed after dueTime on the dispatcher, using a <see cref="System.Windows.Threading.DispatcherTimer"/> object.
142 /// </summary>
143 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
144 /// <param name="state">State passed to the action to be executed.</param>
145 /// <param name="action">Action to be executed.</param>
146 /// <param name="dueTime">Relative time after which to execute the action.</param>
147 /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
148 /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
149 public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
150 {
151 if (action == null)
152 throw new ArgumentNullException("action");
153
154 var dt = Scheduler.Normalize(dueTime);
155 if (dt.Ticks == 0)
156 return Schedule(state, action);
157
158 var d = new MultipleAssignmentDisposable();
159
160 var timer = new System.Windows.Threading.DispatcherTimer(
161 #if HAS_DISPATCHER_PRIORITY
162 _priority, _dispatcher
163 #elif DESKTOPCLR40 // BACKWARDS COMPATIBILITY with v1.x
164 System.Windows.Threading.DispatcherPriority.Background, _dispatcher
165 #endif
166 );
167
168 timer.Tick += (s, e) =>
169 {
170 var t = Interlocked.Exchange(ref timer, null);
171 if (t != null)
172 {
173 try
174 {
175 d.Disposable = action(this, state);
176 }
177 finally
178 {
179 t.Stop();
180 action = null;
181 }
182 }
183 };
184
185 timer.Interval = dt;
186 timer.Start();
187
188 d.Disposable = Disposable.Create(() =>
189 {
190 var t = Interlocked.Exchange(ref timer, null);
191 if (t != null)
192 {
193 t.Stop();
194 action = (_, __) => Disposable.Empty;
195 }
196 });
197
198 return d;
199 }
200
201 /// <summary>
202 /// Schedules a periodic piece of work on the dispatcher, using a <see cref="System.Windows.Threading.DispatcherTimer"/> object.
203 /// </summary>
204 /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
205 /// <param name="state">Initial state passed to the action upon the first iteration.</param>
206 /// <param name="period">Period for running the work periodically.</param>
207 /// <param name="action">Action to be executed, potentially updating the state.</param>
208 /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>
209 /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
210 /// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than TimeSpan.Zero.</exception>
211 public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
212 {
213 if (period < TimeSpan.Zero)
214 throw new ArgumentOutOfRangeException("period");
215 if (action == null)
216 throw new ArgumentNullException("action");
217
218 var timer = new System.Windows.Threading.DispatcherTimer(
219 #if HAS_DISPATCHER_PRIORITY
220 _priority, _dispatcher
221 #elif DESKTOPCLR40 // BACKWARDS COMPATIBILITY with v1.x
222 System.Windows.Threading.DispatcherPriority.Background, _dispatcher
223 #endif
224 );
225
226 var state1 = state;
227
228 timer.Tick += (s, e) =>
229 {
230 state1 = action(state1);
231 };
232
233 timer.Interval = period;
234 timer.Start();
235
236 return Disposable.Create(() =>
237 {
238 var t = Interlocked.Exchange(ref timer, null);
239 if (t != null)
240 {
241 t.Stop();
242 action = _ => _;
243 }
244 });
245 }
246 }
247 }
248 #endif