I have a Presence monitor class which is used to detect users active/inactive status. That class has a timer in its Start method which called on application start:
public class PresenceMonitor { private volatile bool _running; private Timer _timer; private readonly TimeSpan _presenceCheckInterval = TimeSpan.FromMinutes(1); public PresenceMonitor() { } public void Start() { // Start the timer _timer = new Timer(_ => { Check(); }, null, TimeSpan.Zero, _presenceCheckInterval); } private void Check() { if (_running) { return; } _running = true; // Dowork } } The "Check" method is fired after every one minute. That piece of code is working fine but now my "Do work" methods have become async await so I had to change this Presence Monitor class to something like this:
public class PresenceMonitor { private volatile bool _running; private Timer _timer; private readonly TimeSpan _presenceCheckInterval = TimeSpan.FromMinutes(1); public PresenceMonitor() { } public void Start() { // Start the timer var timer = new System.Threading.Timer(async (e) => { await CheckAsync(); }, null, TimeSpan.Zero, _presenceCheckInterval); } private async Task CheckAsync() { if (_running) { return; } _running = true; // await DoworkAsync } } Unfortunately "CheckAsync" method now is getting fired once only instead of every minute. Can you tell me what I am doing wrong here to call async await after regular intervals?
Is there any correct way to do the same?