there is a code snippet
int readen = 1; int i = 0; while (readen > 0) { if (msList.Count >= 20) { Thread.Sleep(200); continue; } else { msList.Add(new MemoryStream()); gzsList.Add(i, new GZipStream(msList.Last(), CompressionMode.Compress)); readen = fsIn.Read(buffer, 0, buffer.Length); compressTasks.Add(new Task(() => { (gzsList[i] as GZipStream).Write(buffer, 0, readen); })); i++; compressTasks.Last().Start(); } } the problem is that in the line compressTasks.Add(new Task(() => { (gzsList[i] as GZipStream).Write(buffer, 0, readen); })); incomprehensible things happen: if the condition is met, the thread does not fall asleep, and execution goes to this line and, accordingly, an exception is thrown out, moreover, depending on the type of the gzslist collection (for List``System.InvalidOperationException KeyNotFoundException NullReferenceException . List``System.InvalidOperationException , Dictionary KeyNotFoundException , Hashtable NullReferenceException . If you remove the condition, then all exceptions are equal, but with a different size of the collection (depends on the launch (from 20 to 50)
Taskas streams? Why not modern async / await? - VladDTask'to the collection should lead to a sleep stream. - VladDibetweenTask's, this is a time bomb (think why). - VladD