I have a simple recursive method that calls itself while retrieving messages form an Azure Service Bus Queue. The method adds the messages to a List of messages and then, when there are no more messages in the queue, it sends the List back to it's parent method to be processed.
Under normal circumstances, when all is running well, the queue will never have more that a couple messages in it, as we are constantly pulling messages from the queue just as fast as they arrive. But in theory, if our processor goes down, the messages could stack up in a hurry, and the number of messages could become massive.
I'd like to put an artificial limit on the number of messages that will be pulled out of the queue and put into the List before we gracefully abort the process, send back a batch of messages to be processed, and then keep going again. But I'm not sure of the best way to do this. I could create an integer and add to it each call, checking it's value, and then kick out of the process when it reaches a certain point. But, is there a better way?