C# 定时器
C/C++ Code复制内容到剪贴板
- System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- t.Enabled = true;
- t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
- t.AutoReset = true; //每到指定时间Elapsed事件是触发一次(false),还是一直触发(true)
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string sql = "select * from UserInfo"; // 查询语句
- string getResult = SynchronousParkData.QueryToJson(sql);
- t.Stop();
- System.Diagnostics.Debug.WriteLine("未到指定时间5秒提前终结!!!");
- }
- private void Timer_TimesUp(object sender, System.Timers.ElapsedEventArgs e)
- {
- //到达指定时间5秒触发该事件
- System.Diagnostics.Debug.WriteLine("Hello World!!!!");
- }