C#Winform窗体工具类(七)定时器SetTimeout和SetInterval

文章属于转载,小部分修改,具体查看文章 :CODE:给c#添加SetTimeout和SetInterval函数

链接 https://www.cnblogs.com/wuchang/archive/2009/02/19/1096496.html


调用

 FormTools.SetTimeout(()=>
                {
                    this.web.Visible = true;
                    InitLoading();
                },1000);
var i=1;
var inter = new Action(() =>
            {
                MessageBox.Show("第"+i + "次重复执行");
               i++;
            });
FormTools.SetInterval(inter, 800);

具体方法

       #region 定时执行

        /// <summary>
        /// 在指定时间过后执行指定的表达式
        /// </summary>
        /// <param name="interval">时间(以毫秒为单位)</param>
        /// <param name="action">要执行的表达式</param>
        /// <return>返回timer对象</return>
        public static Timer SetTimeout(Action action, double interval)
        {
            var timer = new Timer(interval);
            timer.Elapsed += (sender, e) =>
            {
                timer.Enabled = false;
                action();
            };
            timer.Enabled = true;
            return timer;
        }

        /// <summary>
        /// 在指定时间周期重复执行指定的表达式
        /// </summary>
        /// <param name="interval">时间(以毫秒为单位)</param>
        /// <param name="action">要执行的表达式</param>
        public static void SetInterval(Action action, double interval)
        {
            var timer = new Timer(interval);
            timer.Elapsed += (sender, e) => { action(); };
            timer.Enabled = true;
        }

        #endregion

猜你喜欢

转载自blog.csdn.net/qq_28254093/article/details/80726512