| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Reactive.Disposables; |
| 4 |
using System.Threading; |
| 5 |
using System.Windows.Forms; |
| 6 |
|
| 7 |
namespace System.Reactive.Concurrency |
| 8 |
{ |
| 9 |
/// <summary> |
| 10 |
/// Represents an object that schedules units of work on the message loop associated with a Windows Forms control. |
| 11 |
/// </summary> |
| 12 |
public class ControlScheduler : LocalScheduler, ISchedulerPeriodic |
| 13 |
{ |
| 14 |
private readonly Control _control; |
| 15 |
|
| 16 |
/// <summary> |
| 17 |
/// Constructs a ControlScheduler that schedules units of work on the message loop associated with the specified Windows Forms control. |
| 18 |
/// </summary> |
| 19 |
/// <param name="control">Windows Forms control to get the message loop from.</param> |
| 20 |
/// <exception cref="ArgumentNullException"><paramref name="control"/> is null.</exception> |
| 21 |
/// <remarks> |
| 22 |
/// This scheduler type is typically used indirectly through the <see cref="System.Reactive.Linq.ControlObservable.ObserveOn<TSource>"/> and <see cref="System.Reactive.Linq.ControlObservable.SubscribeOn<TSource>"/> method overloads that take a Windows Forms control. |
| 23 |
/// </remarks> |
| 24 |
public ControlScheduler(Control control) |
| 25 |
{ |
| 26 |
if (control == null) |
| 27 |
throw new ArgumentNullException("control"); |
| 28 |
|
| 29 |
_control = control; |
| 30 |
} |
| 31 |
|
| 32 |
/// <summary> |
| 33 |
/// Gets the control associated with the ControlScheduler. |
| 34 |
/// </summary> |
| 35 |
public Control Control |
| 36 |
{ |
| 37 |
get { return _control; } |
| 38 |
} |
| 39 |
|
| 40 |
/// <summary> |
| 41 |
/// Schedules an action to be executed on the message loop associated with the control. |
| 42 |
/// </summary> |
| 43 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 44 |
/// <param name="state">State passed to the action to be executed.</param> |
| 45 |
/// <param name="action">Action to be executed.</param> |
| 46 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 47 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 48 |
public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) |
| 49 |
{ |
| 50 |
if (action == null) |
| 51 |
throw new ArgumentNullException("action"); |
| 52 |
|
| 53 |
var d = new SingleAssignmentDisposable(); |
| 54 |
|
| 55 |
_control.BeginInvoke(new Action(() => |
| 56 |
{ |
| 57 |
if (!d.IsDisposed) |
| 58 |
d.Disposable = action(this, state); |
| 59 |
})); |
| 60 |
|
| 61 |
return d; |
| 62 |
} |
| 63 |
|
| 64 |
/// <summary> |
| 65 |
/// Schedules an action to be executed after dueTime on the message loop associated with the control, using a Windows Forms Timer object. |
| 66 |
/// </summary> |
| 67 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 68 |
/// <param name="state">State passed to the action to be executed.</param> |
| 69 |
/// <param name="action">Action to be executed.</param> |
| 70 |
/// <param name="dueTime">Relative time after which to execute the action.</param> |
| 71 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 72 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 73 |
public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 74 |
{ |
| 75 |
if (action == null) |
| 76 |
throw new ArgumentNullException("action"); |
| 77 |
|
| 78 |
var dt = Scheduler.Normalize(dueTime); |
| 79 |
if (dt.Ticks == 0) |
| 80 |
return Schedule(state, action); |
| 81 |
|
| 82 |
var createTimer = new Func<IScheduler, TState, IDisposable>((scheduler1, state1) => |
| 83 |
{ |
| 84 |
var d = new MultipleAssignmentDisposable(); |
| 85 |
|
| 86 |
var timer = new System.Windows.Forms.Timer(); |
| 87 |
|
| 88 |
timer.Tick += (s, e) => |
| 89 |
{ |
| 90 |
var t = Interlocked.Exchange(ref timer, null); |
| 91 |
if (t != null) |
| 92 |
{ |
| 93 |
try |
| 94 |
{ |
| 95 |
d.Disposable = action(scheduler1, state1); |
| 96 |
} |
| 97 |
finally |
| 98 |
{ |
| 99 |
t.Stop(); |
| 100 |
action = null; |
| 101 |
} |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
timer.Interval = (int)dt.TotalMilliseconds; |
| 106 |
timer.Start(); |
| 107 |
|
| 108 |
d.Disposable = Disposable.Create(() => |
| 109 |
{ |
| 110 |
var t = Interlocked.Exchange(ref timer, null); |
| 111 |
if (t != null) |
| 112 |
{ |
| 113 |
t.Stop(); |
| 114 |
action = (_, __) => Disposable.Empty; |
| 115 |
} |
| 116 |
}); |
| 117 |
|
| 118 |
return d; |
| 119 |
}); |
| 120 |
|
| 121 |
// |
| 122 |
// This check is critical. When creating and enabling a Timer object on another thread than |
| 123 |
// the UI thread, it won't fire. |
| 124 |
// |
| 125 |
if (_control.InvokeRequired) |
| 126 |
return Schedule(state, createTimer); |
| 127 |
else |
| 128 |
return createTimer(this, state); |
| 129 |
} |
| 130 |
|
| 131 |
/// <summary> |
| 132 |
/// Schedules a periodic piece of work on the message loop associated with the control, using a Windows Forms Timer object. |
| 133 |
/// </summary> |
| 134 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 135 |
/// <param name="state">Initial state passed to the action upon the first iteration.</param> |
| 136 |
/// <param name="period">Period for running the work periodically.</param> |
| 137 |
/// <param name="action">Action to be executed, potentially updating the state.</param> |
| 138 |
/// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns> |
| 139 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 140 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than one millisecond.</exception> |
| 141 |
public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action) |
| 142 |
{ |
| 143 |
// |
| 144 |
// Threshold derived from Interval property setter in ndp\fx\src\winforms\managed\system\winforms\Timer.cs. |
| 145 |
// |
| 146 |
if (period.TotalMilliseconds < 1) |
| 147 |
throw new ArgumentOutOfRangeException("period"); |
| 148 |
if (action == null) |
| 149 |
throw new ArgumentNullException("action"); |
| 150 |
|
| 151 |
var createTimer = new Func<IScheduler, TState, IDisposable>((scheduler1, state1) => |
| 152 |
{ |
| 153 |
var timer = new System.Windows.Forms.Timer(); |
| 154 |
|
| 155 |
timer.Tick += (s, e) => |
| 156 |
{ |
| 157 |
state1 = action(state1); |
| 158 |
}; |
| 159 |
|
| 160 |
timer.Interval = (int)period.TotalMilliseconds; |
| 161 |
timer.Start(); |
| 162 |
|
| 163 |
return Disposable.Create(() => |
| 164 |
{ |
| 165 |
var t = Interlocked.Exchange(ref timer, null); |
| 166 |
if (t != null) |
| 167 |
{ |
| 168 |
t.Stop(); |
| 169 |
action = _ => _; |
| 170 |
} |
| 171 |
}); |
| 172 |
}); |
| 173 |
|
| 174 |
// |
| 175 |
// This check is critical. When creating and enabling a Timer object on another thread than |
| 176 |
// the UI thread, it won't fire. |
| 177 |
// |
| 178 |
if (_control.InvokeRequired) |
| 179 |
return Schedule(state, createTimer); |
| 180 |
else |
| 181 |
return createTimer(this, state); |
| 182 |
} |
| 183 |
} |
| 184 |
} |