Timers

Reliable Actor timers are modelled on timers available in the system.Timers namespace. The timers are usually used to carry out routine operations in the lifetime of an Actor, for example processing input data at a certain rate. You can declare and register a timer in your application using the following code, usually in the OnActivateAsync method:

private IActorTimer _updateTimer; 
protected override Task OnActivateAsync()
{
...

_updateTimer = RegisterTimer(
CallBackMethod, // Callback method
ObjectParameter, // Parameter to pass to
the callback method
TimeSpan.FromMilliseconds(15), // Amount of time to delay
before the callback is
invoked
TimeSpan.FromMilliseconds(15)); // Time interval between
invocations of the
callback method

return base.OnActivateAsync();
}

The CallBack method is simply a function that accepts the parameter that you passed while declaring the timer:

    private Task CallBackMethod(object state) 
{
...
return Task.FromResult(true);
}

Finally, you can unregister a timer using the UnregisterTimer method:

protected override Task OnDeactivateAsync() 
{
if (_updateTimer != null)
{
UnregisterTimer(_updateTimer);
}
return base.OnDeactivateAsync();
}

Because of the turn wise concurrency feature of Reliable Actors, no two concurrent executions of the callback method will take place at any time. The time will be stopped while the callback is executing and will be restarted when it has completed.

It is important to note that if the Actor doesn't receive external invocations, it will be deactivated and garbage collected after a period of time. When this happens, the timers won't be activated any more. Therefore, it is important that you register the timer again in the OnActivateAsync method when the Actor is reactivated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset