Skip to content

Commit ffc086e

Browse files
akarnokdOren Novotny
authored andcommitted
4.x: Have the ControlScheduler do more eager disposed checks (#529)
* 4.x: Have the ControlScheduler do more eager disposed checks * Fix style
1 parent 85c7d65 commit ffc086e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Concurrency/ControlScheduler.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,16 @@ public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TSta
5252
if (action == null)
5353
throw new ArgumentNullException(nameof(action));
5454

55+
if (_control.IsDisposed)
56+
{
57+
return Disposable.Empty;
58+
}
59+
5560
var d = new SingleAssignmentDisposable();
5661

5762
_control.BeginInvoke(new Action(() =>
5863
{
59-
if (!d.IsDisposed)
64+
if (!_control.IsDisposed && !d.IsDisposed)
6065
d.Disposable = action(this, state);
6166
}));
6267

@@ -94,7 +99,10 @@ public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Fun
9499
{
95100
try
96101
{
97-
d.Disposable = action(scheduler1, state1);
102+
if (!_control.IsDisposed && !d.IsDisposed)
103+
{
104+
d.Disposable = action(scheduler1, state1);
105+
}
98106
}
99107
finally
100108
{
@@ -156,7 +164,10 @@ public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<
156164

157165
timer.Tick += (s, e) =>
158166
{
159-
state1 = action(state1);
167+
if (!_control.IsDisposed)
168+
{
169+
state1 = action(state1);
170+
}
160171
};
161172

162173
timer.Interval = (int)period.TotalMilliseconds;

0 commit comments

Comments
 (0)