private void btnCancel_Click(object sender, EventArgs e) { if (timer != null) { timer.Stop(); } string timeStr = realDate.Year + DateTime.Now.ToString("-MM-dd HH:mm:ss"); SysTimePro.SetLocalTimeByStr(timeStr); this.Close(); }
void ProcessMethod(int timeLength, int year) { int timespan = 20; // 20 毫秒 int count = timeLength * (1000 / timespan); // 20 秒 Console.WriteLine("倒计时 " + count / (1000 / timespan) + " 秒;"); string currentYear = DateTime.Now.Year.ToString(); timer = new Timer(timespan); timer.Elapsed += (sender, args) => { DateTime now = DateTime.Now; if (now.Year != year) { //SysTimePro.SetTimeByCmd("2008",now.Month.ToString(),now.Day.ToString()); string timeStr = year + now.ToString("-MM-dd HH:mm:ss"); SysTimePro.SetLocalTimeByStr(timeStr); } if (--count == 0) { timer.Stop(); string timeStr = currentYear + now.ToString("-MM-dd HH:mm:ss"); SysTimePro.SetLocalTimeByStr(timeStr); System.Environment.Exit(1); } else { if (count % (1000 / timespan) == 0) { this.Invoke(new Action(() => { lbLeftTime.Text = (count / (1000 / timespan)).ToString(); })); } } }; timer.Start(); }