c # set the timer to automatically execute the event after a certain time

public static void SetTimeOut(double interval, Action action)
        {
            System.Timers.Timer timer = new System.Timers.Timer(interval);//实例化Timer类,设置间隔时间;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(delegate (object sender, System.Timers.ElapsedEventArgs e)
            {
                timer.Enabled = false;
                action();
            });//到达时间的时候执行事件;
            timer.AutoReset = false;//设置是执行一次(false)还是一直执行(true);
            timer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
        }

timer.Interval: set the time interval, by way of example may be of the Timer class, set the time interval;
Action: to perform expression;
the Elapsed: execution event;
Enabled: whether System.Timers.Timer.Elapsed event;
AutoReset: setting false, execution time, true, has been executed.

Published 51 original articles · won praise 11 · views 6090

Guess you like

Origin blog.csdn.net/weixin_42140261/article/details/102838112