Skip to content

Commit f90205d

Browse files
authored
Use UserWorkItems in ThreadPoolScheduler.Windows and save some allocations. (#650)
1 parent de57ac9 commit f90205d

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

Rx.NET/Source/src/System.Reactive/Concurrency/ThreadPoolScheduler.Windows.cs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,16 @@ public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TSta
7373
if (action == null)
7474
throw new ArgumentNullException(nameof(action));
7575

76-
var d = new SingleAssignmentDisposable();
76+
var userWorkItem = new UserWorkItem<TState>(this, state, action);
77+
78+
var res = global::Windows.System.Threading.ThreadPool.RunAsync(
79+
iaa => userWorkItem.Run(),
80+
Priority,
81+
Options);
7782

78-
var res = global::Windows.System.Threading.ThreadPool.RunAsync(iaa =>
79-
{
80-
if (!d.IsDisposed)
81-
{
82-
d.Disposable = action(this, state);
83-
}
84-
}, Priority, Options);
85-
86-
return new CompositeDisposable(
87-
d,
88-
Disposable.Create(res.Cancel)
89-
);
83+
userWorkItem.CancelQueueDisposable = Disposable.Create(res.Cancel);
84+
85+
return userWorkItem;
9086
}
9187

9288
/// <summary>
@@ -115,23 +111,15 @@ public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Fun
115111

116112
private IDisposable ScheduleSlow<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
117113
{
118-
var d = new SingleAssignmentDisposable();
114+
var userWorkItem = new UserWorkItem<TState>(this, state, action);
119115

120116
var res = global::Windows.System.Threading.ThreadPoolTimer.CreateTimer(
121-
tpt =>
122-
{
123-
if (!d.IsDisposed)
124-
{
125-
d.Disposable = action(this, state);
126-
}
127-
},
128-
dueTime
129-
);
130-
131-
return new CompositeDisposable(
132-
d,
133-
Disposable.Create(res.Cancel)
134-
);
117+
tpt => userWorkItem.Run(),
118+
dueTime);
119+
120+
userWorkItem.CancelQueueDisposable = Disposable.Create(res.Cancel);
121+
122+
return userWorkItem;
135123
}
136124

137125
/// <summary>

0 commit comments

Comments
 (0)