Beispiel #1
0
            private void ExecuteInternal()
            {
                Debug.Assert(_operation >= Operation.Read && _operation <= Operation.WriteGather);

                long      result    = 0;
                Exception?exception = null;

                try
                {
                    // This is the operation's last chance to be canceled.
                    if (_cancellationToken.IsCancellationRequested)
                    {
                        exception = new OperationCanceledException(_cancellationToken);
                    }
                    else
                    {
                        switch (_operation)
                        {
                        case Operation.Read:
                            Memory <byte> writableSingleSegment = MemoryMarshal.AsMemory(_singleSegment);
                            result = RandomAccess.ReadAtOffset(_fileHandle, writableSingleSegment.Span, _fileOffset);
                            break;

                        case Operation.Write:
                            RandomAccess.WriteAtOffset(_fileHandle, _singleSegment.Span, _fileOffset);
                            break;

                        case Operation.ReadScatter:
                            Debug.Assert(_readScatterBuffers != null);
                            result = RandomAccess.ReadScatterAtOffset(_fileHandle, _readScatterBuffers, _fileOffset);
                            break;

                        case Operation.WriteGather:
                            Debug.Assert(_writeGatherBuffers != null);
                            RandomAccess.WriteGatherAtOffset(_fileHandle, _writeGatherBuffers, _fileOffset);
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    exception = e;
                }
                finally
                {
                    _operation          = Operation.None;
                    _context            = null;
                    _cancellationToken  = default;
                    _singleSegment      = default;
                    _readScatterBuffers = null;
                    _writeGatherBuffers = null;
                }

                if (exception == null)
                {
                    _source.SetResult(result);
                }
                else
                {
                    _source.SetException(exception);
                }
            }