當我打開Cellule.cs類時,會創建一個計時器,當Cellule.cs關閉時,計時器仍會調用表單。計時器不會收集垃圾
我該如何有效地處置它。這會導致一個問題,因爲表單Cellules.Cs經常打開,並且每次調用TimeHasChanged()都會對數據庫進行一次調用;
我已經嘗試在Btimer中添加一個Dispose()方法並將計時器設置爲null,但它不能解決問題。
private BTimer timer;
public Cellules()
{
timer = new BTimer(30000);
timer.TheTimeChanged += TimeHasChanged;
}
protected void TimeHasChanged()
{
controller.UpdateTimeMesPieces();
lblTime.Text = controller.UpdateTimeClock();
}
而且繼承人的計時器
public class BTimer
{
private string theTime;
private Timer timer;
public BTimer(int interval)
{
timer = new Timer();
timer.Tick += new EventHandler(Timer_Tick);
timer.Interval = interval;
timer.Start();
}
public delegate void TimerTickHandler();
public event TimerTickHandler TheTimeChanged;
public string TheTime
{
get
{
return theTime;
}
set
{
theTime = value;
OnTheTimeChanged();
}
}
protected void OnTheTimeChanged()
{
if (TheTimeChanged != null)
{
TheTimeChanged();
}
}
private void Timer_Tick(object sender, EventArgs e)
{
TheTime = "Bing";
}
}
代碼不能編譯:'BTimer'沒有實現' IDisposable' – Blorgbeard
@Blorgbeard對不起,我刪除它,這是從我最後一次嘗試解決它 – Insecurefarm