Reputation: 145
I am using Service Bus of azure which will hold my list of messages means messages can enter in service bus at any time.
So i want to keep a watch on my service bus to check that whether any message is there in service or not.It is like i just want to keep a watch on my service bus regarding message arrival in my service bus at interval 0f 20 seconds.
In Every 20 Seconds i would like to check message arrival in my service bus and this i want to perform in background asynchronously.
I want to call below method in every 20 seconds in background:
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();
}
So can anybody tell me how do i call this above method every 20 seconds?
Upvotes: 0
Views: 885
Reputation: 845
The most beginner-friendly solution is:
Drag a Timer from the Toolbox, give it a Name, set your desired Interval and set "Enabled" to True. Then double click the Timer and Visual Studio (or whatever you are using) will write you the following Code:
private void wait_Tick(object sender, EventArgs e)
{
refreshText(); //add the method you want to call here.
}
or this
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()
}
Upvotes: 1