private void Run() { bool success = false; try { using (FileStream writer = new FileStream(_filePath, FileMode.Create, FileAccess.Write)) { int bytesWritten = 0; while (bytesWritten < _targetFileSize) { CancelableValue <ArraySegment <byte> > nextProcessMsg = _fileSlices.Take(); if (nextProcessMsg.Cancel) { // Todo: remove partial file return; } ArraySegment <byte> fileSlice = nextProcessMsg.Value; writer.Write(fileSlice); bytesWritten += fileSlice.Count; } success = bytesWritten == _targetFileSize; } } catch (Exception e) { Log.Error("Could not write file: {exception}", e); } finally { _runFinally?.Invoke(success); } }
public void Cancel() => _fileSlices.Add(CancelableValue <ArraySegment <byte> > .CreateCanceledValue());
public void Process(ArraySegment <byte> fileSlice) => _fileSlices.Add(CancelableValue <ArraySegment <byte> > .CreateWithValue(fileSlice));