Skip to content

Instantly share code, notes, and snippets.

@eouw0o83hf
Last active August 29, 2015 14:13
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 eouw0o83hf/7dfa97a5e08d245d0538 to your computer and use it in GitHub Desktop.
Save eouw0o83hf/7dfa97a5e08d245d0538 to your computer and use it in GitHub Desktop.
Throttle Helper
public class Throttle
{
private Task _control;
private readonly int _minimumMillis;
public Throttle(int millis)
{
_minimumMillis = millis;
}
public void Block()
{
// Only block if it's not the first time
if(_control != null)
{
_control.Wait();
}
_control = Task.Delay(_minimumMillis);
}
}
public static class ThrottleExtensions
{
public static IEnumerable<T> Throttled<T>(this IEnumerable<T> source, int millis)
{
var throttle = new Throttle(millis);
foreach(var item in source)
{
throttle.Block();
yield return item;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment