From 81fc53cf482850b18eadfd5d358e9d2ccc142f6a Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Thu, 17 May 2018 14:38:07 +0200 Subject: [PATCH] ConcurrencyAbstractionLayerImpl.FastPeriodicTimer and PeriodicTimer: Allow delegate caching. --- .../ConcurrencyAbstractionLayerImpl.cs | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayerImpl.cs b/Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayerImpl.cs index 9a92e55b3..648effb9d 100644 --- a/Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayerImpl.cs +++ b/Rx.NET/Source/src/System.Reactive/Concurrency/ConcurrencyAbstractionLayerImpl.cs @@ -194,13 +194,18 @@ public PeriodicTimer(Action action, TimeSpan period) _action = action; // - // Rooting of the timer happens through the this.Tick delegate's target object, + // Rooting of the timer happens through the timer's state // which is the current instance and has a field to store the Timer instance. // - _timer = new System.Threading.Timer(Tick, null, period, period); + _timer = new System.Threading.Timer(_ => Tick(_), this, period, period); } - private void Tick(object state) => _action(); + private static void Tick(object state) + { + var timer = (PeriodicTimer)state; + + timer._action(); + } public void Dispose() { @@ -224,19 +229,21 @@ public FastPeriodicTimer(Action action) { _action = action; - new System.Threading.Thread(Loop) + new System.Threading.Thread(_ => Loop(_)) { Name = "Rx-FastPeriodicTimer", IsBackground = true } - .Start(); + .Start(this); } - private void Loop() + private static void Loop(object threadParam) { - while (!disposed) + var timer = (FastPeriodicTimer)threadParam; + + while (!timer.disposed) { - _action(); + timer._action(); } }