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(); // 開始 } } }