System.Waf.Foundation.ThrottledAction.InvokeAccumulated C# (CSharp) Method

InvokeAccumulated() public method

Requests the execution of the action delegate.
public InvokeAccumulated ( ) : void
return void
        public void InvokeAccumulated()
        {
            lock (timerLock)
            {
                if (mode == ThrottledActionMode.InvokeOnlyIfIdleForDelayTime || !isRunning)
                {
                    isRunning = true;
                    timer.Change(delayTime, Timeout.InfiniteTimeSpan);
                }
            }
        }

Usage Example

コード例 #1
0
ファイル: ThrottledActionTest.cs プロジェクト: jbe2277/waf
        public void InvokeOnlyIfIdleForDelayTimeWithoutSynchronizationContext()
        {
            Assert.IsNull(SynchronizationContext.Current);

            int actionCallCount = 0;
            var throttledAction = new ThrottledAction(() => Interlocked.Add(ref actionCallCount, 1), ThrottledActionMode.InvokeOnlyIfIdleForDelayTime, TimeSpan.FromMilliseconds(100));
            Assert.IsFalse(throttledAction.IsRunning);

            throttledAction.InvokeAccumulated();
            Assert.IsTrue(throttledAction.IsRunning);
            throttledAction.InvokeAccumulated();
            throttledAction.InvokeAccumulated();

            // Multiple calls of InvokeAccumulated within the delayTime should call the action just once.
            Task.Delay(200).Wait();
            Assert.AreEqual(1, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);

            actionCallCount = 0;
            throttledAction.InvokeAccumulated();
            Task.Delay(60).Wait();
            throttledAction.InvokeAccumulated();
            Task.Delay(60).Wait();
            throttledAction.InvokeAccumulated();
            Task.Delay(60).Wait();
            throttledAction.InvokeAccumulated();

            // Calls just once: The waits between InvokeAccumulated are less than the idle (delay) time.
            Task.Delay(200).Wait();
            Assert.AreEqual(1, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);
        }
All Usage Examples Of System.Waf.Foundation.ThrottledAction::InvokeAccumulated