Skip to content

Commit

Permalink
ConcurrencyAbstractionLayerImpl.FastPeriodicTimer and PeriodicTimer: …
Browse files Browse the repository at this point in the history
…Allow delegate caching.
  • Loading branch information
danielcweber committed May 17, 2018
1 parent 1ddb618 commit 81fc53c
Showing 1 changed file with 15 additions and 8 deletions.
Expand Up @@ -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()
{
Expand All @@ -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();
}
}

Expand Down

0 comments on commit 81fc53c

Please sign in to comment.