示例#1
0
        public async Task MultipleThreads()
        {
            var runs = 0;

            Action action = () =>
            {
                runs++;
            };

            var throttler = new ActionThrottler(action, TimeSpan.FromMilliseconds(50));

            new Thread(() => throttler.Call()).Start();
            new Thread(() => throttler.Call()).Start();
            new Thread(() => throttler.Call()).Start();

            await Task.WhenAll(
                Task.Factory.StartNew(() => throttler.Call()),
                Task.Factory.StartNew(() => throttler.Call()),
                Task.Factory.StartNew(() => throttler.Call()),
                Task.Run(() => throttler.Call()),
                Task.Run(() => throttler.Call()),
                Task.Run(() => throttler.Call()));

            await Task.Delay(TimeSpan.FromSeconds(0.5));

            var calls = throttler.Calls;

            Assert.IsTrue(runs == 1 && throttler.Calls == 9);
        }
示例#2
0
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            var p = e.Location;

            mousePosition = new PointF(p.X, p.Y);
            mouseMoveThrottler.Call();
        }
示例#3
0
        public async Task SameThread()
        {
            var runs = 0;

            Action action = () =>
            {
                runs++;
            };

            var throttler = new ActionThrottler(action, TimeSpan.FromMilliseconds(50));

            throttler.Call();
            throttler.Call();
            throttler.Call();
            throttler.Call();
            throttler.Call();
            throttler.Call();

            await Task.Delay(TimeSpan.FromSeconds(0.5));

            Assert.IsTrue(runs == 1 && throttler.Calls == 6);
        }