I am trying to monitor if a DB record is updated by an outside process. So I would like to check if the record is updated every 3 seconds, and timeout after 30 seconds if it has not updated.
My current approach uses a Timer to check the status periodically and a Stopwatch to check for Timeout, but I'm not sure if this is the best approach since my DB check function and update record function are asynchronous.
private System.Timers.Timer aTimer;
public async Task MyCode()
{
    aTimer = new System.Timers.Timer { Interval = 3000 };
    var stopWatch = new Stopwatch(); // to control the timeout
    // Hook up the Elapsed event for the timer. 
    aTimer.Elapsed += async (sender, e) => await 
    VerifyRecordIsUpdatedAsync(sender, e, recordId);
    // Have the timer fire repeated events (true is the default)
    aTimer.AutoReset = true;
    // Start the timer
    aTimer.Enabled = true;
    while (aTimer.Enabled)
    {
        var ts = stopWatch.Elapsed;
        if (ts.TotalSeconds > 30)
        {
           aTimer.Stop();
        }
    }
    await ProcessFunctionAsync()
}
private async Task VerifyRecordIsUpdatedAsync(object sender, ElapsedEventArgs e, Guid recordId)
{
    var recordDb = await _context.Records.FirstOrDefaultAsync(x => x.Id == recordId);
    if (recordDb.Status == Status.Updated)
    {
        aTimer.Stop();
    }
}
 
    