如何每隔20秒检查一次到达服务总线的消息?

时间:2015-09-30 10:00:27

标签: c# azure scheduled-tasks azureservicebus servicebus

我正在使用azure的 Service Bus ,这将保留我的消息列表,这意味着消息可以随时进入服务总线。

所以我想在我的服务总线上看一下,检查是否有任何消息在服务中。就像我只想在服务总线上看一下有关消息到达服务总线的时间间隔0秒20秒。

在每20秒钟我想检查消息到达我的服务总线,我想在后台异步执行。

我想在后台每隔20秒调用以下方法:

 private static void ReceiveMessages()
        {
            // For PeekLock mode (default) where applications require "at least once" delivery of messages 
            SubscriptionClient agentSubscriptionClient = SubscriptionClient.Create(TopicName, "AgentSubscription");
            BrokeredMessage message = null;
            while (true)
            {
                try
                {
                    //receive messages from Agent Subscription
                    message = agentSubscriptionClient.Receive(TimeSpan.FromSeconds(5));
                    if (message != null)
                    {
                        Console.WriteLine("\nReceiving message from AgentSubscription...");
                        Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
                        // Further custom message processing could go here...
                        message.Complete();
                    }
                    else
                    {
                        //no more messages in the subscription
                        break;
                    }
                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }
                    else
                    {
                        HandleTransientErrors(e);
                    }
                }
            }

            // For ReceiveAndDelete mode, where applications require "best effort" delivery of messages
            SubscriptionClient auditSubscriptionClient = SubscriptionClient.Create(TopicName, "AuditSubscription", ReceiveMode.ReceiveAndDelete);
            while (true)
            {
                try
                {
                    message = auditSubscriptionClient.Receive(TimeSpan.FromSeconds(5));
                    if (message != null)
                    {
                        Console.WriteLine("\nReceiving message from AuditSubscription...");
                        Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
                        // Further custom message processing could go here...
                    }
                    else
                    {
                        Console.WriteLine("\nno more messages in the subscription");
                        //no more messages in the subscription
                        break;
                    }

                }
                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }
                }
            }

            agentSubscriptionClient.Close();
            auditSubscriptionClient.Close();
        }

所以有人能告诉我如何每隔20秒调用一次上述方法吗?

1 个答案:

答案 0 :(得分:1)

最适合初学者的解决方案是:

从工具箱中拖动一个计时器,为其命名,设置所需的间隔并将“已启用”设置为True。然后双击Timer和Visual Studio(或者您正在使用的任何内容)将为您写下以下代码:

private void wait_Tick(object sender, EventArgs e)
{
    refreshText(); //add the method you want to call here.
}

或者

private Timer timer1; 
public void InitTimer()
{
    timer1 = new Timer();
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = 2000; // in miliseconds
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    isonline()
}