Timer.Dispose

  • Thread starter Thread starter RedLars
  • Start date Start date
R

RedLars

Looking for someone to spend a minute to review the below code.

I have a class with a collection of System.Threading.Timers that I
need to Dispose of and be certain they are disposed before
continuing.

The class has the following variable
private List<Timer> timerList = new List<Timer>();

This code is executed an unknown number of times:
Timer t1 = new Timer(timerDelegate, null, 1000, 250);
timerList.Add(t1);


This code tried to dispose of all the timers in the collection

private void DeleteTimers()
{
if (timerList.Count > 0)
{
int index = 0;
WaitHandle[] waitList = new AutoResetEvent[timerList.Count];
foreach (Timer timer in timerList)
{
WaitHandle wh = new AutoResetEvent(false);
waitList.SetValue(wh, index++);
timer.Dispose(wh);
}
WaitHandle.WaitAll(waitList);
timerList.Clear();
}
}

Any help would be much appreciated.
 
Back
Top