设为首页 加入收藏

TOP

延时执行函数:前浪死在沙滩上
2019-09-17 18:23:48 】 浏览:22
Tags:延时 执行 函数 沙滩

业务场景:有主表、子表两个GridView点击主表的行,会自动读取主表对应的子表数据

但是如果反复点击会导致反复读取,其实反复点击的时候只需要最后一次执行查询,前面的几次点击都是无意义操作

根据这一需求设计了一个函数:

private static List<System.Windows.Forms.Timer> Tup = new List<System.Windows.Forms.Timer>();
/// <summary>
/// 延时执行
/// </summary>
/// <param name="easySub">需要执行的代码/函数</param>
/// <param name="keyName">任务分组</param>
/// <param name="timeOut">等待timeOut时间后执行代码,如果当前分组中进入新的任务则前面的任务放弃,执行新的任务</param>
/// <param name="chkStatus">等待一定时间且循环验证chkStatus</param>
public static void DelayRun(Action easySub, string keyName, int timeOut = 501)
{
     System.Windows.Forms.Timer lastTimer = Tup.Find(t => t.Tag.ToStringEx() == keyName);
     if (lastTimer != null)
     {
         lastTimer.Enabled = false;
         lastTimer.Enabled = true;
     }

    if (lastTimer == null)
     {
         System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
         t.Interval = timeOut;
         t.Enabled = true;
         t.Tag = keyName;
         Tup.Add(t);
         t.Tick += delegate (object sender, EventArgs e)
         {
             ((System.Windows.Forms.Timer)sender).Enabled = false;
             string name = keyName;
             easySub();
         };
     }
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.NET Core 常用第三方包 下一篇【.NET Core项目实战-统一认证平..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目