我做了一個窗口窗體應用程序,按下按鈕每5分鐘執行一次該方法,然後再按一次停止這樣做......但它仍然繼續執行該方法,即使我打電話給停止方法從計時器。爲什麼我不能停止我的計時器
System.Timers.Timer t = new System.Timers.Timer(TimeSpan.FromMinutes(5).TotalMilliseconds);
t.AutoReset = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(my_method);
if (start == false)
{
t.Start();
start = true;
Checkbutton.Text = "End";
}
else
{
t.Stop();
t.AutoReset = false;
Checkbutton.Text = "Begin";
MessageBox.Show("Auto Check Stop!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
是不是你創建這個功能的新的計時器每次?將Timer t作爲你的類的私有變量,並且在開始時移動t = new Timer stuff == false條件;就在t.Start()之前。 – Developer