{
public class TsRun : IDisposable
{
private bool alreadydisposed = false;
private System.Threading.Timer ThreadTimer;
public TsRun()
{
if (ThreadTimer == null)
{
ThreadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(RFID_Read), null, 0, 1000);
}
}
private void Stop()
{
if (ThreadTimer != null)
{
alreadydisposed = true;
ThreadTimer.Dispose();
ThreadTimer = null;
}
}
#region dispose
~TsRun()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!alreadydisposed)
{
return;
}
if (disposing)
{
//释放托管资源
//託管資源:由CLR管理分配和釋放的資源,即由CLR裡new出來的物件;
Stop();
}
//释放非托管资源
alreadydisposed = true;
}
#endregion dispose
}
}
參考: Effective C# — .NET資源管理