Timer.Dispose

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top