コード例 #1
0
        public void SendMessage(Action <Stream> action, CancellationToken cancellationToken)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (cancellationToken.IsCancellationRequested)
            {
                LastMessageWasCancelled = true;

                return;
            }

            LastMessageWasCancelled = false;
            _messageCancelledEvent.Reset();
            _messageSendingEvent.Set();

            using (var stream = new MemoryMappedOutputStream(this, cancellationToken))
            {
                try
                {
                    action(stream);
                }
                catch
                {
                    LastMessageWasCancelled = true;
                    _messageCancelledEvent.Set();

                    throw;
                }
            }

            var index = WaitHandle.WaitAny(new[] { _messageReadEvent, _messageCancelledEvent, cancellationToken.WaitHandle });

            if (index == 1 || index == 2)
            {
                LastMessageWasCancelled = true;
            }
        }
コード例 #2
0
        public void SendMessage(Action<Stream> action, CancellationToken cancellationToken)
        {
            if (_isDisposed)
                throw new ObjectDisposedException(GetType().FullName);

            if (cancellationToken.IsCancellationRequested)
            {
                LastMessageWasCancelled = true;

                return;
            }

            LastMessageWasCancelled = false;
            _messageCancelledEvent.Reset();
            _messageSendingEvent.Set();

            using (var stream = new MemoryMappedOutputStream(this, cancellationToken))
            {
                try
                {
                    action(stream);
                }
                catch
                {
                    LastMessageWasCancelled = true;
                    _messageCancelledEvent.Set();

                    throw;
                }
            }

            var index = WaitHandle.WaitAny(new[] {_messageReadEvent, _messageCancelledEvent, cancellationToken.WaitHandle});

            if (index == 1 || index == 2)
            {
                LastMessageWasCancelled = true;
            }
        }