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(); } }