public void Run() { var options = new ThrottlerOptions { DelayMillis = 20, RatePerSecond = 1000, InitialQuota = 0, MaxBurstSize = 100 }; var throttler = new ThrottlerThread(options); throttler.Start(); var second = -1; var count = 0; while (true) { if (second != DateTime.Now.Second) { second = DateTime.Now.Second; WriteLine($" - {count} times"); Write($"{second:D2}: "); count = 0; } throttler.Throttle(); if (count % 20 == 0) { Write("."); } count++; } }
public void Run() { var options = new ThrottlerOptions { DelayMillis = 20, RatePerSecond = 1000, InitialQuota = 0, MaxBurstSize = 100 }; var throttler = new ThrottlerThread(options); throttler.Start(); var second = -1; var count = 0; var lesEnumerables = Enumerable.Range(0, 10000); var throtteledEnumerables = lesEnumerables.Throttle(throttler); foreach (var i in throtteledEnumerables) { if (second != DateTime.Now.Second) { second = DateTime.Now.Second; WriteLine($" - {count} times"); Write($"{second:D2}: "); count = 0; } count++; } throttler.Stop(); WriteLine($"done!"); }
private static void ChangeThrottlerState(ThrottlerThread throttler) { if (throttler == null) { return; } switch (throttler.State) { case ThrottlerState.Started: throttler.Pause(); break; case ThrottlerState.Stopped: break; case ThrottlerState.Paused: throttler.Resume(); break; case ThrottlerState.Unkown: break; default: throw new ArgumentOutOfRangeException(); } }
public static IEnumerable Throttle(this IEnumerable self, ThrottlerThread throttler) { if (throttler == null) { throw new ArgumentException(@"Exception occured while throttling because the throttler object was null", nameof(throttler)); } foreach (var item in self) { throttler.Throttle(); yield return(item); } }
public void Run() { var options = new ThrottlerOptions { DelayMillis = 20, RatePerSecond = 1000, InitialQuota = 0, MaxBurstSize = 100 }; var throttler = new ThrottlerThread(options); throttler.Start(); var second = -1; var count = 0; Task.Run(() => { while (true) { if (second != DateTime.Now.Second) { second = DateTime.Now.Second; WriteLine($" - {count} times"); Write($"{second:D2}: "); count = 0; } throttler.Throttle(); count++; } // ReSharper disable once FunctionNeverReturns }); while (true) { var command = ReadKey(true); if (command.Key != ConsoleKey.Enter) { continue; } Write($"{Environment.NewLine}changing throttler state: "); ChangeThrottlerState(throttler); WriteLine($"throttler is now {throttler.State}"); } }