コード例 #1
0
        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);
            }
        }
コード例 #2
0
 public void Cancel() => _fileSlices.Add(CancelableValue <ArraySegment <byte> > .CreateCanceledValue());
コード例 #3
0
 public void Process(ArraySegment <byte> fileSlice) =>
 _fileSlices.Add(CancelableValue <ArraySegment <byte> > .CreateWithValue(fileSlice));