C# WinForm定时触发事件

时间:2015-01-19 20:28:14   收藏:0   阅读:496
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // 时钟定时器
        System.Timers.Timer timer = new System.Timers.Timer(); // 实例化

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString()); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            timer.Interval = 1000; // 间隔时间
            timer.Elapsed += new System.Timers.ElapsedEventHandler(button1_Click); // 到时间后执行
            timer.AutoReset = true; // 是否一直执行
            timer.Enabled = true; // 是否执行
            timer.Start(); // 开始
        }
    }
}

 

原文:http://www.cnblogs.com/mtsl/p/4234503.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!