C# 自定义定时器的开启、暂停、继续

一、创建定时器

private System.Windows.Forms.Timer timerGetTime = new System.Windows.Forms.Timer();//创建定时器

public enum RunState//按钮状态 1运行中 2暂停  3停止
        {
            running,
            pause,
            stop
        }

RunState runstate = RunState.stop;

二、初始化定时器设值

 //设置定时器属性
 timerGetTime.Interval = 5000; //间隔时间
 timerGetTime.Enabled = false;//先不开启
 //定时器关联事件
 timerGetTime.Tick += new EventHandler(TimeExecute);

三、点击开启按钮

private void button1_Click(object sender, EventArgs e)//开启按钮
        {

            button2.Text = "暂停";//暂停继续按钮
            button2.Enabled = true;//暂停继续按钮禁用

            timerGetTime.Enabled = true;

            timerGetTime.Start();

            runstate = RunState.runni

你可能感兴趣的:(C#,c#,开发语言,winform)