public override void Processing() { _calculationInProgress = true; _writer = new Thread(() => WriteToFile()); _writer.Start(); using (FileStream _inputFileStream = new FileStream(_inputParameters.SourceFileName, FileMode.Open)) { int dataRead; byte[] buffer = new byte[_chunkSize]; int currentThread = 0; int currentBlock = 0; while ((dataRead = _inputFileStream.Read(buffer, 0, buffer.Length)) > 0) { if (_stopProcess) { break; } currentBlock++; while (true) { if (_stopProcess) { break; } if (_threadReady[currentThread]) { _threadReady[currentThread] = false; if ((_threads[currentThread] != null) && _threads[currentThread].ThreadState.Equals(ThreadState.Running)) { _threads[currentThread].Join(); } byte[] copyBuffer = new byte[buffer.Length]; buffer.CopyTo(copyBuffer, 0); IThreadBlock cbuf = new ThreadBlock(currentBlock, currentThread, copyBuffer, dataRead); _threads[currentThread] = new Thread(() => BlockProcessing(cbuf)); _threads[currentThread].Start(); currentThread = (currentThread + 1) % _threadsNumber; break; } else { currentThread = (currentThread + 1) % _threadsNumber; } } } } for (int i = 0; i < _threadsNumber; i++) { if (_threads[i].ThreadState.Equals(ThreadState.Running)) { _threads[i].Join(); } } _calculationInProgress = false; _writer.Join(); }
public override void Processing() { _calculationInProgress = true; _writer = new Thread(() => WriteToFile()); _writer.Start(); using (FileStream _inputFileStream = new FileStream(_inputParameters.SourceFileName, FileMode.Open)) { int currentThread = 0; int currentBlock = 0; while (_inputFileStream.Position < _inputFileStream.Length) { if (_stopProcess) { break; } currentBlock++; byte[] lengthBytes = new byte[4]; _inputFileStream.Read(lengthBytes, 0, 4); var lengthCompressed = BitConverter.ToInt32(lengthBytes, 0); byte[] _compressedBuffer = new byte[lengthCompressed]; _inputFileStream.Read(lengthBytes, 0, 4); var lengthOriginal = BitConverter.ToInt32(lengthBytes, 0); _inputFileStream.Read(_compressedBuffer, 0, lengthCompressed); while (true) { if (_stopProcess) { break; } if (_threadReady[currentThread]) { _threadReady[currentThread] = false; if ((_threads[currentThread] != null) && _threads[currentThread].ThreadState.Equals(ThreadState.Running)) { _threads[currentThread].Join(); } byte[] copyBuffer = new byte[_compressedBuffer.Length]; _compressedBuffer.CopyTo(copyBuffer, 0); IThreadBlock cbuf = new ThreadBlock(currentBlock, currentThread, copyBuffer, lengthOriginal); _threads[currentThread] = new Thread(() => BlockProcessing(cbuf)); _threads[currentThread].Start(); currentThread = (currentThread + 1) % _threadsNumber; break; } else { currentThread = (currentThread + 1) % _threadsNumber; } } } } for (int i = 0; i < _threadsNumber; i++) { if ((_threads[i] != null) && _threads[i].ThreadState.Equals(ThreadState.Running)) { _threads[i].Join(); } } _calculationInProgress = false; _writer.Join(); }