View Code
System.Timers.Timer t; DateTime lastInputTime = DateTime.MinValue; private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e) { if (t!=null) { t.Stop(); } t = new System.Timers.Timer(); t.Interval = 3000; t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件; t.AutoReset = false;//设置是执行一次(false)还是一直执行(true); t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; //if (lastInputTime == DateTime.MinValue) //{ // lastInputTime = DateTime.Now; //} //else //{ // var nowInputTime = DateTime.Now; // if ((nowInputTime - lastInputTime).Seconds > 3) // { // MessageBox.Show("大于"); // } // lastInputTime = nowInputTime; //} } public void theout(object source, System.Timers.ElapsedEventArgs e) { MessageBox.Show(e.SignalTime.ToString()); }