当我打开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