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. FluentValidation

    git :https://github.com/JeremySkinner/FluentValidation Example using FluentValidation; public class ...

  2. js函数中this的不同含义

    1.js函数调用过程中,js线程会进入新的执行环境并创建该环境的变量对象,并添加两个变量:this和arguments,因此可以在函数中使用这两个变量.需要注意的是,this变量不能重新赋值,而arg ...

  3. qt creator 源代码中含有中文编译报错

    Tools-Options-Text Editor-Behavior-File Encoding-Default encoding:UTF-8 Tools-Options-Text Editor-Be ...

  4. 如何让页眉随章节的不同而变化(Word 2010)

    在一般情况下,我们将页眉设置完成后,所有章节的页眉都是一样的. 但在某些时候,我们需要让不同的章节拥有各自不同的页眉.那么该如何设置呢? 1. 我们以下图的文件为例,该文件一共包括4个实验,暂未设置页 ...

  5. LeetCode之383. Ransom Note

    -------------------------------------------- 思路就是进行频率统计. 统计一下第二个字符串字符出现次数++统计一下第一个字符串中字符出现次数--如果出现负数 ...

  6. MSSQL 查询表空间

    1. exec sp_spaceused '表名'            (SQL统计数据,大量事务操作后可能不准) 2. exec sp_spaceused '表名', true       (更新 ...

  7. 代理模式/proxy模式/结构型模式

    代理模式proxy 定义 为其他对象提供一种代理,并以控制对这个对象的访问.最简单的理解,买东西都是要去商店的,不会去工厂. java实现三要素 proxy(代理)+subject(接口)+realS ...

  8. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  9. 破解压缩文件密码rarcrack

    破解压缩文件密码rarcrack   常见的压缩文件格式有ZIP.RAR和7z.这三种格式都支持使用密码进行加密压缩.前面讲过破解ZIP压缩文件,可以使用fcrackzip.对于RAR和7z格式,可以 ...

  10. [NOIP2012]借教室 题解

    题目大意: 有一个n个数的数列,m个操作,第i个操作使[li,ri]区间建di,问第几个操作使数列中出现负数. 思路: 暴力显然过不了,那么就可以优化了,不难想到线段树,显然需要良好的姿势,那么就差分 ...