private void ScrollingThread() { var sleepWaitHandles = new WaitHandle[] { startScrollEvent, exitScrollThreadEvent }; var exitLoopWaitHandles = new WaitHandle[] { stopScrollEvent, exitScrollThreadEvent }; var stopWatch = new Stopwatch(); long remainingDelay; // The first loop, sleep/run loop. for (;;) { WaitHandle.WaitAny(sleepWaitHandles); if (exitScrollThreadEvent.WaitOne(0)) { return; } // The second loop, scrolling loop. while (round <= totalRounds && WaitHandle.WaitAny(exitLoopWaitHandles, 0) == WaitHandle.WaitTimeout) { if (exitScrollThreadEvent.WaitOne(0)) { return; } var stepLength = CalculateScrollDistance(); if (stepLength == 0) { break; } lock (locker) { ++round; remain -= stepLength; } stopWatch.Restart(); pageScroller.Scroll(direction, stepLength); stopWatch.Stop(); remainingDelay = Math.Max(Interval - stopWatch.ElapsedMilliseconds, MinInterval); // Delay. WaitHandle.WaitAny(exitLoopWaitHandles, (int)remainingDelay); } CleanupScroll(); } }
private void ScrollingThread(object obj) { while (round <= totalRounds) { var stepLength = CalculateScrollDistance(); if (stepLength == 0) { break; } lock (locker) { round += 1; remain -= stepLength; } pageScroller.Scroll(direction, stepLength); Thread.Sleep(Interval); } CleanupScroll(); }