C#下的防抖-Debounce、节流阀-Throttle功能实现

防抖-Debounce

连续的多次调用,只有在调用停止之后的一段时间内不再调用,然后才执行一次处理过程。

节流阀-Throttle

连续的多次调用,在每个时间段的周期内只执行第一次处理过程。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using System.ComponentModel; namespace HZ.Common
{
public class DelayAction
{
Timer _timerDbc;
Timer _timerTrt; /// <summary>
/// 延迟timesMs后执行。 在此期间如果再次调用,则重新计时
/// </summary>
/// <param name="invoker">同步对象,一般为Control控件。 如不需同步可传null</param>
public void Debounce(int timeMs, ISynchronizeInvoke invoker, Action action)
{
lock (this)
{
if (_timerDbc == null)
{
_timerDbc = new Timer(timeMs);
_timerDbc.AutoReset = false;
_timerDbc.Elapsed += (o, e) =>
{
_timerDbc.Stop();
_timerDbc.Close();
_timerDbc = null;
InvokeAction(action, invoker);
};
}
_timerDbc.Stop();
_timerDbc.Start();
}
} /// <summary>
/// 即刻执行,执行之后,在timeMs内再次调用无效
/// </summary>
/// <param name="timeMs">不应期,这段时间内调用无效</param>
/// <param name="invoker">同步对象,一般为控件。 如不需同步可传null</param>
public void Throttle(int timeMs, ISynchronizeInvoke invoker, Action action)
{
System.Threading.Monitor.Enter(this);
bool needExit = true;
try
{
if (_timerTrt == null)
{
_timerTrt = new Timer(timeMs);
_timerTrt.AutoReset = false;
_timerTrt.Elapsed += (o, e) =>
{
_timerTrt.Stop();
_timerTrt.Close();
_timerTrt = null;
};
_timerTrt.Start();
System.Threading.Monitor.Exit(this);
needExit = false;
InvokeAction(action, invoker);//这个过程不能锁
}
}
finally
{
if (needExit)
System.Threading.Monitor.Exit(this);
}
} /// <summary>
/// 延迟timesMs后执行。
/// </summary>
public void Delay(int timeMs, ISynchronizeInvoke invoker, Action action)
{
Debounce(timeMs, invoker, action);
} private static void InvokeAction(Action action, ISynchronizeInvoke invoker)
{
if (invoker == null)
{
action();
}
else
{
if (invoker.InvokeRequired)
{
invoker.Invoke(action, null);
}
else
{
action();
}
}
}
}
}

C#.Net下的防抖-Debounce和节流阀-Throttle功能实现的更多相关文章

  1. 防抖debounce和节流throttle

    大纲 一.出现缘由 二.什么是防抖debounce和节流throttle 三.应用场景 3.1防抖 3.2节流 一.出现缘由 前端开发中,有一部分用户行为会频繁触发事件,而对于DOM操作,资源加载等耗 ...

  2. Java版的防抖(debounce)和节流(throttle)

    概念 防抖(debounce) 当持续触发事件时,一定时间段内没有再触发事件,事件处理函数才会执行一次,如果设定时间到来之前,又触发了事件,就重新开始延时. 防抖,即如果短时间内大量触发同一事件,都会 ...

  3. js 函数的防抖(debounce)与节流(throttle)

    原文:函数防抖和节流: 序言: 我们在平时开发的时候,会有很多场景会频繁触发事件,比如说搜索框实时发请求,onmousemove, resize, onscroll等等,有些时候,我们并不能或者不想频 ...

  4. js 函数的防抖(debounce)与节流(throttle) 带 插件完整解析版 [helpers.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         函数防抖与节流是做什么的?下面进行通俗的讲解. 本文借鉴:h ...

  5. 防抖(Debounce)与节流( throttle)区别

    http://www.cnblogs.com/ShadowLoki/p/3712048.html http://blog.csdn.net/tina_ttl/article/details/51830 ...

  6. js 防抖 debounce 与 节流 throttle

    debounce(防抖) 与 throttle(节流) 主要是用于用户交互处理过程中的性能优化.都是为了避免在短时间内重复触发(比如scrollTop等导致的回流.http请求等)导致的资源浪费问题. ...

  7. JavaScript 防抖(debounce)和节流(throttle)

    防抖函数 触发高频事件后,n秒内函数只会执行一次,如果n秒内高频事件再次被触发,则重新计算时间 /** * * @param {*} fn :callback function * @param {* ...

  8. [JavaScript] 节流(throttle)-防抖(debounce) 不懵圈指北

    网易云课堂 > 微专业 > 前端高级开发工程师 01.前端高级-JavaScript进阶 > 3.函数式编程 Underscore源码分析 > 3.4.3 throttle 与 ...

  9. JavaScript 高级系列之节流 [throttle] 与防抖 [debounce]

    一.概念 这两个东西都是为了项目优化而出现的,官方是没有具体定义的,他们的出现主要是为了解决一些短时间内连续执行的事件带来性能上的不佳和内存的消耗巨大等问题:像这类事件一般像 scroll keyup ...

随机推荐

  1. ubuntu下安装mcrypt

    sudo apt-get install libmcrypt4 php5-mcrypt 一句命令搞定

  2. 如何在github下载开源项目到本地(Coding iOS 客户端为例)

    一.前言 以 Coding iOS 客户端 为例讲解如何在github下载开源项目到本地 github地址:https://github.com/Coding/Coding-iOS 二.分析 根据项目 ...

  3. 简单粗暴将sqlserver表以及数据迁移到oracle

    1.利用sqlserver工具查询出表中所有数据,全选,右键 -连同标题一起复制. 2.将数据保存到excel文件,数据 时间类型(yyyy--MM--dd HH:mm:ss)最好处理一下,需要将id ...

  4. Mac Pro 16G 安装MyEclipse提示虚拟内存(为0)不够

    百度一下很多人都说开多一点程序,让程序占满内存,使其虚拟内存使用就能通过这一步骤,但这里有个更好一点的方案 通过执行: memory_pressure -l critical 用系统内存压力测试进程占 ...

  5. JAVA中static关键字

    用法:是一个修饰符,用于修饰成员(成员变量,成员函数),不能用于修饰局部变量!被static修饰后,就多了一种调用方式,除了可以被对象调用外,还可以直接被类名调用,写法格式是:类名.静态成员.优点:被 ...

  6. 常见开发需求之js处理url汉字编码中的乱码

    需求及解决    两个页面传值的需求是很常见的,angular中有很多常见的方法用于传值,而且都不会受到字符编码的影响,而采用传统的url中拼字符串进行传值的操作,如果拼串中涉及到中文字符,我们就要考 ...

  7. CentOS6.7安装Python3.4

    1.下载Python3.4安装包 wget https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tgz 2.解压.编译.安装 .tgz cd Py ...

  8. Query Mobile学习笔记

    1.获取jQuery mobile 文件,访问jQuerymobile网站下载 (貌似使用jquery mobile后,jquery会自动在网页中添加一些class类,第一次知道的我是被吓呆的!!) ...

  9. MySQL-多条件拼接语句

    BEGIN "; SET @_where=""; THEN SET @_where= CONCAT(@_where," AND sourcedomain=\&q ...

  10. ZeroMQ接口函数之 :zmq_proxy – 开始ZMQ内置代理

    ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq-proxy zmq_proxy(3)             ØMQ Manual - ØMQ/4.1.0 Nam ...