简体   繁体   中英

Call function few seconds after scroll form in C#

In form there is a timer and I want disable timer.tick when form scrolling and a few seconds after that again timer be enabled. How it is possible? Best regards.

You do not need to stop the timer from ticking. You just need to prevent it from doing anything if someone has scrolled recently.

Write a timestamp each time a scroll occurs. You can do this by subscribing to the Scroll event.

DateTime _lastScrollTime = DateTime.MinValue;

void MyControl_Scroll(object sender ScrollEventArgs e)
{
    _lastScrollTime = DateTime.Now;
}

Then in your timer event, check the timestamp to make sure nobody has scrolled recently before doing anything:

void Timer_Tick(object sender, EventArgs e)
{
    if (_lastScrollTime > DateTime.Now.AddSeconds(-2)) return;

    //Do timer stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM