The code throws
NullReferenceException: Object reference not set to an instance of an object
on the line ((ManualResetEvent)handles[i]).Set()
. I checked handles[i] has a value when I debug it. What am I doing wrong?
` string[] fileEntries = Directory.GetFiles(pathFife);
ManualResetEvent[] handles = new ManualResetEvent[fileEntries.Count()];
int i = 0;
foreach (string fullName in fileEntries)
{
handles[i] = new ManualResetEvent(false);
var thread = new Thread(() =>
{
AddFile(fullName, month, year, user);
((ManualResetEvent)handles[i]).Set();
});
thread.Start();
i++;
}
WaitHandle.WaitAll(handles);`