Example #1
0
        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!");
        }
Example #3
0
        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}");
            }
        }