Skip to content

Instantly share code, notes, and snippets.

@ChrisMissal
Last active October 24, 2017 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisMissal/90f8eb2de43a9341404d41f23a5d60f8 to your computer and use it in GitHub Desktop.
Save ChrisMissal/90f8eb2de43a9341404d41f23a5d60f8 to your computer and use it in GitHub Desktop.
public class ConsistentInterval : IDisposable
{
private readonly TimeSpan delay;
private readonly CancellationToken cancellationToken;
private readonly DateTimeOffset startTime;
public ConsistentInterval(TimeSpan delay, CancellationToken cancellationToken)
{
this.delay = delay;
this.cancellationToken = cancellationToken;
this.startTime = DateTimeOffset.UtcNow;
}
public void Dispose()
{
var delayTime = this.startTime.Add(this.delay).Subtract(DateTimeOffset.UtcNow);
Task.Delay(delayTime, this.cancellationToken).GetAwaiter().GetResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment