using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Timers; using System.Runtime.InteropServices; using System.Threading; namespace Timer001 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //实例化Timer类 System.Timers.Timer aTimer = new System.Timers.Timer(); private void button1_Click(object sender, EventArgs e) { this.SetTimerParam(); } private void test(object source, System.Timers.ElapsedEventArgs e) { MessageBox.Show(DateTime.Now.ToString()); } public void SetTimerParam() { //到时间的时候执行事件 aTimer.Elapsed += new ElapsedEventHandler(test); aTimer.Interval = 1000; aTimer.AutoReset = true;//执行一次 false,一直执行true //是否执行System.Timers.Timer.Elapsed事件 aTimer.Enabled = true; } } }
实现的效果是:每秒弹出系统当前时间,如下图:
原文地址:http://blog.csdn.net/jiankunking/article/details/38090155