protected static void ProcessMessages(IAzureQueue<T> queue, IEnumerable<T> messages, Func<T, bool> action)
        {
            if (queue == null)
            {
                throw new ArgumentNullException("queue");
            }

            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (messages == null)
            {
                throw new ArgumentNullException("messages");
            }

            foreach (var message in messages)
            {
                var allowDelete = false;
                var corruptMessage = false;

                try
                {
                    allowDelete = action(message);
                }
                catch (Exception ex)
                {
                    TraceHelper.TraceError(ex.TraceInformation());
                    corruptMessage = true;
                }
                finally
                {
                    if (allowDelete || (corruptMessage && message.GetMessageReference().DequeueCount > 5))
                    {
                        try
                        {
                            queue.DeleteMessage(message);
                        }
                        catch (Exception ex)
                        {
                            TraceHelper.TraceError(
                                "Error deleting message type '{0}' id {1}: {2}", 
                                message.GetType().Name,
                                message.GetMessageReference().Id,
                                ex.TraceInformation());
                        }
                    }
                }
            }
        }
        protected static void ProcessMessages(IAzureQueue <T> queue, IAzureQueue <T> deadQueue, IEnumerable <T> messages, Action <T> action)
        {
            if (queue == null)
            {
                throw new ArgumentNullException("queue");
            }

            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (messages == null)
            {
                throw new ArgumentNullException("messages");
            }

            foreach (var message in messages)
            {
                var success = false;

                try
                {
                    action(message);
                    success = true;
                }
                catch (Exception)
                {
                    success = false;
                }
                finally
                {
                    if (deadQueue != null && message.DequeueCount > 5)
                    {
                        deadQueue.AddMessage(message);
                    }
                    if (success || message.DequeueCount > 5)
                    {
                        queue.DeleteMessage(message);
                    }
                }
            }
        }