public void WriteChunks(CountdownEvent cdEvent) { using (_outputStorage) { while (cdEvent.CurrentCount != 0) { _threadHelper.WaitForWriting(); var chunk = _threadHelper.TakeChunk().Data; if (_isCompressed) { _outputStorage.WriteInt(chunk.Length); } _outputStorage.WriteBytes(chunk); cdEvent.Signal(); _maxCountEvent.Signal(); GC.Collect(); OnEndOfIteration?.Invoke(); } } _resetEvent.Set(); }