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. C# Bitmap deep copy

    今天在研究一个关于 Bitmap deep copy 的问题, 经过一系列的查询,在StackOverFlow上面找到了答案,遂记录下来: public static Bitmap DeepCopyB ...

  2. SQL Server 2008登录错误:无法连接到(local)解决方法

    在一些朋友安装完SQL Server 2008之后大多会遇到连接出错的问题比如:SQL Server 2008登录错误:无法连接到(local)等等相关问题,本文将详细介绍解决方法,需要的朋友可以参考 ...

  3. RSA加密(C语言)

    /** * \file rsa.h * * \brief The RSA public-key cryptosystem * * Copyright (C) 2006-2010, Brainspark ...

  4. linux升级openssh

    升级sshd到OpenSSH-6.7并删除老版本ssh 1)升级前准备 查看是否缺包 # rpm -qa | egrep "gcc|make|perl|pam|pam-devel" ...

  5. app上线具体流程

    前言:前两天有个同事问到我APP上传的问题,几个地方我竟然模糊不清了,查了些资料在这里整理一下.鄙人是一个懒猿,一直以来都是从各位大神的博客中汲取营养,却懒于写自己的.越来越觉得写博客对于一个开发者来 ...

  6. C语言笔记一

    学习C语言已经有一段时间,然而发现越学不知道的东西越多,这是在印象笔记中记得一些东西,现在再回顾一遍顺便补充一些新东西. 一,基础知识 运算符号  优先级  单目>算术>关系 从高到低   ...

  7. 【Jsoup网页解析】

    下载链接:http://jsoup.org/download 一.普通的请求方式(不带有cookie) 使用举例: 第一步: Connection conn=Jsoup.connect(url); 第 ...

  8. windows批处理语法

    写批处理文件,除了了解基本语法外,你还需要熟悉常用的windows命令,那就先看看这篇文章:windows常用命令 #重要说明 文件及目录路径:要使用反斜杠'\',不要使用正斜杠'/' 如:del d ...

  9. 【CentOS】压缩打包

    一.gzip [-d][-1-9][filename] -d  解压 -[1-9]  压缩等级(默认为6) zcat filename.gz 查看压缩文件 最小化安装centOS是没有安装bzip2的 ...

  10. iOS 之 Cocoapods安装

    进入正题前,先来点前奏:了解cocoapods是某天看一个博客,那时才明白原来写项目不用一个个将三方库拷进项目里啊,惊讶的我是一塌糊涂的啊...(原谅我那时还没进入过正规的IT公司....好多你们自然 ...