System.Timers.Timer

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Test
{
    public partial class Form1 : Form
    {
        System.Timers.Timer timerTest1 = new System.Timers.Timer();

        private static void timefunc(object state)
        {
            Trace.WriteLine(string.Format("into Timer1_Elapsed,{0}", DateTime.Now.ToString()));
            System.Threading.Thread.Sleep(3000);

            Trace.WriteLine(string.Format("exit Timer1_Elapsed,{0}", DateTime.Now.ToString()));
        }

        public Form1()
        {
            InitializeComponent();

            timerTest1.Interval = 1;  // 立即执行
            timerTest1.Elapsed += Timer1_Elapsed;
            timerTest1.AutoReset = true;
        }

        private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timerTest1.Interval = 2000000;  // 设置比较大的值,以便重新计时
            Trace.WriteLine(string.Format("into Timer1_Elapsed,{0}", DateTime.Now.ToString()));
            System.Threading.Thread.Sleep(10000);
            Trace.WriteLine(string.Format("exit Timer1_Elapsed,{0}", DateTime.Now.ToString()));
            timerTest1.Interval = 1000;    // 重新计时开始
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timerTest1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/jshchg/p/12950853.html
今日推荐