计时器不会收集垃圾
Timer does not get garbage collected
当我打开classCellule.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";
}
}
如果 class 包含一个一次性成员,class 也应该实现 IDisposable
注意:BTimer需要实现IDisposable。
class Cellules : IDisposable
{
BTimer timer // disposable member
public void Dispose() // implementing IDisposable
{
timer.Dispose();
}
}
当然,当你完成后,在 Cellules 的任何实例上调用 Dispose
回应您的编辑
您想在 BTimer 上实现 IDisposable class
public class BTimer : IDisposable
{
private string theTime;
private Timer timer;
........
void Dispose()
{
timer.Stop();
timer.Dispose();
}
}
我要添加的是 BTimer 的 Stop 方法,因此您无需处理即可停止计时器。
您的 BTimer
尚未实现 IDisposable,您必须在不再需要后注销 TheTimeChanged
事件。
您做需要实现IDisposable
,并且在您的Dispose
方法中您需要Dispose
计时器,而不仅仅是设置它为 `null.
您可能还应该解除任何事件与计时器的关联(尽管如果计时器的 Dispose
方法执行此操作,这可能是不必要的):
timer.Tick -= Timer_Tick;
timer.Dispose();
当我打开classCellule.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";
}
}
如果 class 包含一个一次性成员,class 也应该实现 IDisposable
注意:BTimer需要实现IDisposable。
class Cellules : IDisposable
{
BTimer timer // disposable member
public void Dispose() // implementing IDisposable
{
timer.Dispose();
}
}
当然,当你完成后,在 Cellules 的任何实例上调用 Dispose
回应您的编辑
您想在 BTimer 上实现 IDisposable class
public class BTimer : IDisposable
{
private string theTime;
private Timer timer;
........
void Dispose()
{
timer.Stop();
timer.Dispose();
}
}
我要添加的是 BTimer 的 Stop 方法,因此您无需处理即可停止计时器。
您的 BTimer
尚未实现 IDisposable,您必须在不再需要后注销 TheTimeChanged
事件。
您做需要实现IDisposable
,并且在您的Dispose
方法中您需要Dispose
计时器,而不仅仅是设置它为 `null.
您可能还应该解除任何事件与计时器的关联(尽管如果计时器的 Dispose
方法执行此操作,这可能是不必要的):
timer.Tick -= Timer_Tick;
timer.Dispose();