這是我1分鐘後調用事件的代碼,每隔1分鐘後有兩個按鈕啓動並調用已過去的事件,第二個按鈕用於停止啓動此定時器過期事件。停止計時器已按下按鈕單擊的事件
啓動按鈕EVNT代碼
protected void Button1_Click(object sender, EventArgs e)
{
System.Timers.Timer _timer;
_timer = new System.Timers.Timer(10000);
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Enabled = true;
}
定時器經過的事件
static void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
//It works fine it fires after every minute
}
停止按鈕事件代碼
protected void Button2_Click(object sender, EventArgs e)
{
//What to do here??
}
我想停止這個定時器活動停止單擊此按鈕evnent。這裏應該是什麼代碼?
我的整個代碼是
public partial class TimerScheduler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
static void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
}
System.Timers.Timer _timer = new System.Timers.Timer(10000);
//_timer = ;
protected void Button1_Click(object sender, EventArgs e)
{
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Enabled = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
_timer.Stop();
_timer.Enabled = false;
}
}
'_timer.Stop()',不是嗎? –
_timer.Stop()或_timer.Enabled = false – Heslacher
我試過這個 System.Timers.Timer _timer; (對象發件人,EventArgs e) _timer = new System.Timers.Timer(10000); _timer.Elapsed + = new ElapsedEventHandler(_timer_Elapsed); _timer.Enabled = true; } protected void Button2_Click(object sender,EventArgs e) { _timer.Stop(); } 當我點擊停止按鈕時 未將對象引用設置爲對象的實例。:( – Jahangeer