Ejemplo n.º 1
0
        /// <summary>
        /// Asynchronously performs the examples tasks in a loop until the
        /// <paramref name="token"/> is canceled.
        /// </summary>
        /// <returns>
        /// A task that when complete contains a tuple with the number of
        /// messages published and the number of messages received in total.
        /// </returns>
        static async Task <(int messagesSent, int messagesReceived)> RunLoopsAsync(
            IQueuedMessageSession session,
            CancellationToken token)
        {
            Console.WriteLine("Run Host.vi to send and receive messages from this example.");
            Console.WriteLine("Press Ctrl+C to stop.");
            Console.WriteLine();

            /*
             * Begin each loop and wait for both to complete.
             */
            var publishLoop = PublishLoopAsync(session, token);
            var readLoop    = ReadMessageLoopAsync(session, token);
            await Task.WhenAll(publishLoop, readLoop).ConfigureAwait(false);

            return(publishLoop.Result, readLoop.Result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Asynchronously reads messages from Host.vi until
        /// <paramref name="token"/> is canceled.
        /// </summary>
        /// <returns>A task that when complete contains the number of messages
        /// read by the loop.</returns>
        static async Task <int> ReadMessageLoopAsync(IQueuedMessageSession session, CancellationToken token)
        {
            int messagesReceived = 0;

            while (!token.IsCancellationRequested)
            {
                /*
                 * Try to read a message if one is available. The timeout
                 * determines the responsiveness of stopping the example,
                 * because we wait for the read to complete before returning.
                 */
                var message = await session.ReadAsync(timeoutMilliseconds : 2000)
                              .ConfigureAwait(false);

                if (message == null)
                {
                    // No message has been queued, so loop around to read again.
                    continue;
                }

                ++messagesReceived;
                var date = DateTime.Now.ToString("d", CultureInfo.CurrentCulture);
                var time = DateTime.Now.ToString("T", CultureInfo.CurrentCulture);

                Console.WriteLine("Received message:");
                Console.WriteLine("\tDate: {0}", date);
                Console.WriteLine("\tTime: {0}", time);
                Console.WriteLine("\tTopic: {0}", message.Topic);
                Console.WriteLine("\tMessage: {0}", message.Message);
                Console.WriteLine();

                /*
                 * Send a message back to the host to indicate we received
                 * the message. The reply is sent using a different topic than
                 * the one on which we received the message.
                 */
                await session.PublishAsync(HostTopic,
                                           "Replying to " + message.Message).ConfigureAwait(false);
            }

            return(messagesReceived);
        }