Execute an action after a specific time, but if it is called manually, reset the timer

Viewed 406

I am using System.Timers.Timer to execute an action every 10 secs. This action can also be called by other methods if some special condition arises or through UI. If the action is not called from the timer, I just reset the timer.

The code I am using...

timer = new Timer();
timer.Elapsed += (sender, args) => ExecuteAction();
timer.Interval = 10000;
timer.Enabled = true;

public void ExecuteActionAndResetTimer()
{
    ExecuteAction();

    timer.Stop();
    timer.Start();
}

private void ExecuteAction()
{
    // do the work...
}

The expected result, if 'X' is action called from timer (i.e. ExecuteAction), 'X' is action called from outside timer (i.e. ExecuteActionAndResetTimer) and 'o' is a second:

XooooXoXoXooooXoXooooX

This is working fine. I just want to know that can we do this using reactive extensions?

Thanks.

1 Answers
Related