示例#1
0
        public static Task SendMultipartMessageAsync(
            this NetMQSocket socket,
            NetMQMessage message,
            TimeSpan?timeout = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var timeoutCts = new CancellationTokenSource();

            if (timeout is TimeSpan timeoutNotNull)
            {
                timeoutCts.CancelAfter(timeoutNotNull);
            }

            CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(
                cancellationToken,
                timeoutCts.Token
                );

            return(socket.SendMultipartMessageAsync(
                       message,
                       false,
                       cancellationToken: cts.Token
                       ).ContinueWith(t =>
            {
                try
                {
                    if (t.IsCanceled && timeoutCts.Token.IsCancellationRequested)
                    {
                        throw new TimeoutException(
                            $"The operation exceeded the specified time: {timeout}."
                            );
                    }

                    return t;
                }
                finally
                {
                    timeoutCts.Dispose();
                    cts.Dispose();
                }
            }));
        }
示例#2
0
        public static async Task SendMultipartMessageAsync(
            this NetMQSocket socket,
            NetMQMessage message,
            TimeSpan?timeout = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var cts = new CancellationTokenSource();

            if (timeout is TimeSpan timeoutNotNull)
            {
                cts.CancelAfter(timeoutNotNull);
            }

            var ct = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token);

            try
            {
                await socket.SendMultipartMessageAsync(message, false, cancellationToken : ct.Token);
            }
            catch (TaskCanceledException)
            {
                if (cts.Token.IsCancellationRequested)
                {
                    throw new TimeoutException(
                              $"The operation exceeded the specified time: {timeout}."
                              );
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                cts.Dispose();
                ct.Dispose();
            }
        }