Example #1
0
        /// <summary>
        /// Asynchronously publishes messages to Host.vi with a
        /// <see cref="MessageDelay"/> delay before each message until the
        /// <paramref name="token"/> is canceled.
        /// </summary>
        /// <returns>A task that when complete contains the number of messages
        /// published by the loop.</returns>
        static async Task <int> PublishLoopAsync(IMessageSession session, CancellationToken token)
        {
            int messagesSent = 0;

            while (!token.IsCancellationRequested)
            {
                try
                {
                    await Task.Delay(MessageDelay, token).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    // Return when the token is canceled during the delay.
                    break;
                }

                var message = DateTime.Now.ToString(CultureInfo.CurrentCulture);
                await session.PublishAsync(HostTopic, message).ConfigureAwait(false);

                ++messagesSent;
            }

            return(messagesSent);
        }