From d30137f2c8d98b55565a882963e36171e7a3b39e Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Wed, 20 Jun 2018 17:08:11 +0200 Subject: [PATCH] Use UserWorkItems in ThreadPoolScheduler.Windows and save some allocations. --- .../ThreadPoolScheduler.Windows.cs | 44 +++++++------------ 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/Rx.NET/Source/src/System.Reactive/Concurrency/ThreadPoolScheduler.Windows.cs b/Rx.NET/Source/src/System.Reactive/Concurrency/ThreadPoolScheduler.Windows.cs index b673d8b3f3..598d0f0e95 100644 --- a/Rx.NET/Source/src/System.Reactive/Concurrency/ThreadPoolScheduler.Windows.cs +++ b/Rx.NET/Source/src/System.Reactive/Concurrency/ThreadPoolScheduler.Windows.cs @@ -73,20 +73,16 @@ public override IDisposable Schedule(TState state, Func(this, state, action); + + var res = global::Windows.System.Threading.ThreadPool.RunAsync( + iaa => userWorkItem.Run(), + Priority, + Options); - var res = global::Windows.System.Threading.ThreadPool.RunAsync(iaa => - { - if (!d.IsDisposed) - { - d.Disposable = action(this, state); - } - }, Priority, Options); - - return new CompositeDisposable( - d, - Disposable.Create(res.Cancel) - ); + userWorkItem.CancelQueueDisposable = Disposable.Create(res.Cancel); + + return userWorkItem; } /// @@ -115,23 +111,15 @@ public override IDisposable Schedule(TState state, TimeSpan dueTime, Fun private IDisposable ScheduleSlow(TState state, TimeSpan dueTime, Func action) { - var d = new SingleAssignmentDisposable(); + var userWorkItem = new UserWorkItem(this, state, action); var res = global::Windows.System.Threading.ThreadPoolTimer.CreateTimer( - tpt => - { - if (!d.IsDisposed) - { - d.Disposable = action(this, state); - } - }, - dueTime - ); - - return new CompositeDisposable( - d, - Disposable.Create(res.Cancel) - ); + tpt => userWorkItem.Run(), + dueTime); + + userWorkItem.CancelQueueDisposable = Disposable.Create(res.Cancel); + + return userWorkItem; } ///