.net下防抖函数Debounce实现


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

防抖-Debounce

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

节流阀-Throttle

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

C# 全选
using System;
using System.ComponentModel;
using System.Timers;

namespace JOC.WinFramework.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();
                }
            }
        }
    }
}
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
张国生
上一篇:EFCore官方扩展库
下一篇:devexpress gridview修改单元格值后按回车没有立即刷新Summary,必须移动焦点行才行
评论列表

发表评论

评论内容
昵称:
关联文章

.net函数Debounce实现
jquery函数debounce扩展
函数、节流
JS魔法堂:函数节流(throttle)与函数debounce
SqlServer PIVOT函数快速实现行转列,UNPIVOT实现列转行
.Net 高性能分表分库组件-连接模式原理
vue中异步函数async和await的用法
Task 使用详细[基础操作,异步原则,异步函数,异步模式]
使用Hot Chocolate和.NET 6构建GraphQL应用(2) —— 实体相关功能实现
使用.NET 6开发TodoList应用(15)——实现查询搜索
使用.NET 6开发TodoList应用(25)——实现RefreshToken
使用.NET 6开发TodoList应用(22)——实现缓存
.NET Core 利用委托实现动态流程组装
使用.NET 6开发TodoList应用(26)——实现Configuration和Option的强类型绑定
使用.NET 6开发TodoList应用(16)——实现查询排序
使用.NET 6开发TodoList应用(9)——实现PUT请求
使用.NET 6开发TodoList应用(11)——使用FluentValidation和MediatR实现接口请求验证
使用.NET 6开发TodoList应用(14)——实现查询过滤
使用.NET 6开发TodoList应用(21)——实现API版本控制
使用.NET 6开发TodoList应用(12)——实现ActionFilter

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码