Пример #1
0
        private async Task StartInternal(CancellationToken cancellationToken)
        {
            try
            {
                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var result = Dequeue();
                    if (result != null)
                    {
                        _outputStream.Write(result.Item1, result.Item2, result.Item3);
                    }
                    else
                    {
                        await Task.Delay(50, cancellationToken).ConfigureAwait(false);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                _logger.Debug("QueueStream cancelled");
                TaskCompletion.TrySetCanceled();
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error in QueueStream", ex);
                TaskCompletion.TrySetException(ex);
            }
            finally
            {
                OnClosed();
            }
        }
Пример #2
0
        private async Task StartInternal()
        {
            var cancellationToken = _cancellationToken;

            try
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    _isActive = true;

                    var bytes = Dequeue();
                    if (bytes != null)
                    {
                        await _outputStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        await Task.Delay(50, cancellationToken).ConfigureAwait(false);
                    }
                }

                TaskCompletion.TrySetResult(true);
                _logger.Debug("QueueStream complete");
            }
            catch (OperationCanceledException)
            {
                _logger.Debug("QueueStream cancelled");
                TaskCompletion.TrySetCanceled();
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error in QueueStream", ex);
                TaskCompletion.TrySetException(ex);
            }
            finally
            {
                _isActive = false;

                if (OnFinished != null)
                {
                    OnFinished(this);
                }
            }
        }
Пример #3
0
        public void Write(byte[] bytes, int offset, int count)
        {
            //return _outputStream.WriteAsync(bytes, offset, count, cancellationToken);

            try
            {
                _outputStream.Write(bytes, offset, count);
            }
            catch (OperationCanceledException)
            {
                _logger.Debug("QueueStream cancelled");
                TaskCompletion.TrySetCanceled();
                OnClosed();
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error in QueueStream", ex);
                TaskCompletion.TrySetException(ex);
                OnClosed();
            }
        }
Пример #4
0
        public async Task WriteAsync(byte[] bytes, int offset, int count)
        {
            //return _outputStream.WriteAsync(bytes, offset, count, cancellationToken);
            var cancellationToken = _cancellationToken;

            try
            {
                await _outputStream.WriteAsync(bytes, offset, count, cancellationToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                _logger.Debug("QueueStream cancelled");
                TaskCompletion.TrySetCanceled();
                OnClosed();
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error in QueueStream", ex);
                TaskCompletion.TrySetException(ex);
                OnClosed();
            }
        }