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. cookie 和session 区别

    会话技术: Cookie:客户端技术.将数据保存在客户端浏览器上.Cookie是有大小和个数的限制. Session:服务器端技术.将数据保存在服务器端.Session没有大小和个数限制.Sessio ...

  2. java11

    1:对象数组(掌握) (1)数组既可以存储基本数据类型,也可以存储引用类型.它存储引用类型的时候的数组就叫对象数组. (2)案例: 用数组存储5个学生对象,并遍历数组. 2:集合(Collection ...

  3. VC++ 应用程序无法正常启动0xc0150002

    使用VC++开发软件,编译后的程序打不开,弹出错误框:   使用Dpends Walker查看依赖项,没有什么异常. 然后,右键"计算机",选择"管理",打开计 ...

  4. 使用MyBatis Generator自动创建代码( SSM框架)

    步骤: 1.找到该文件目录 (上图文件下载地址:http://download.csdn.net/download/u014617413/9668872) 2.修改generatorConfig.xm ...

  5. RabbitMQ的几种典型使用场景

    RabbitMQ主页:https://www.rabbitmq.com/ AMQP AMQP协议是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现.它主要包括以下组件: 1.Serve ...

  6. supervisor:How is this different from daemontools ?

    "daemontools has too much focus on security as opposed to being a process manager for my taste. ...

  7. iOS存储的三种方式

    iOS中存储的3中方式: 1.NSUserDefaults 注意存储基本对象类型是没有问题的,但是要存储自定义对象,则要将对象内所有的属性(或是需要存储的属性)序列化,实现NSCoding协议序列化. ...

  8. ZZULI 1876: 蛤玮的项链 Hash + 二分

    Time Limit: 6 Sec  Memory Limit: 128 MBSubmit: 153  Solved: 11 SubmitStatusWeb Board Description 蛤玮向 ...

  9. HDU1003 简单DP

    Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ...

  10. 卸载oracle删除注册表脚本

    一.前言 在我们操作系统中,有时要卸载oracle数据库,每一次都要去删除win下的注册表,为了方便删除注册表的信息,下面通过一种删除注册表快捷的脚本. 二.脚本信息 Windows Registry ...