Ejemplo n.º 1
0
        private bool PollAndHandleMessage(Task[] tasks, int availableIx, Task availableTask)
        {
            CloudQueueMessage message;
            QueueEnvelope     envelope = QueueSupport.GetFromDefaultQueue(out message);

            if (envelope != null)
            {
                Task executing = Task.Factory.StartNew(() => WorkerSupport.ProcessMessage(envelope));
                tasks[availableIx] = executing;
                QueueSupport.CurrDefaultQueue.DeleteMessage(message);
                if (availableTask.Exception != null)
                {
                    ErrorSupport.ReportException(availableTask.Exception);
                }
                return(true);
            }
            else
            {
                if (message != null)
                {
                    QueueSupport.CurrDefaultQueue.DeleteMessage(message);
                    ErrorSupport.ReportMessageError(message);
                }
                GC.Collect();
                return(false);
            }
        }
Ejemplo n.º 2
0
        private static void RunTaskedQueueWorker()
        {
            Task[] tasks = new Task[]
            {
                Task.Factory.StartNew(() => {}),
                Task.Factory.StartNew(() => {}),
                Task.Factory.StartNew(() => {}),
                //Task.Factory.StartNew(() => {}),
                //Task.Factory.StartNew(() => {}),
            };
            bool IsStopped    = false;
            int  initialCount = 0;

            while (true)
            {
                try
                {
                    Task.WaitAny(tasks);
                    if (IsStopped)
                    {
                        Task.WaitAll(tasks);
                        foreach (var task in tasks)
                        {
                            if (task.Exception != null)
                            {
                                ErrorSupport.ReportException(task.Exception);
                            }
                        }
                        break;
                    }
                    int  availableIx;
                    Task availableTask = WorkerSupport.GetFirstCompleted(tasks, out availableIx);
                    CloudQueueMessage message;
                    QueueEnvelope     envelope = QueueSupport.GetFromDefaultQueue(out message);
                    if (envelope != null)
                    {
                        Task executing = Task.Factory.StartNew(() => WorkerSupport.ProcessMessage(envelope));
                        tasks[availableIx] = executing;
                        QueueSupport.CurrDefaultQueue.DeleteMessage(message);
                        if (availableTask.Exception != null)
                        {
                            ErrorSupport.ReportException(availableTask.Exception);
                        }
                    }
                    else
                    {
                        if (message != null)
                        {
                            QueueSupport.CurrDefaultQueue.DeleteMessage(message);
                            ErrorSupport.ReportMessageError(message);
                        }
                        Thread.Sleep(1000);
                    }
                }
                catch (AggregateException ae)
                {
                    foreach (var e in ae.Flatten().InnerExceptions)
                    {
                        ErrorSupport.ReportException(e);
                    }
                    Thread.Sleep(10000);
                    // or ...
                    // ae.Flatten().Handle((ex) => ex is MyCustomException);
                }

                /*
                 * catch (MessagingException e)
                 * {
                 * if (!e.IsTransient)
                 * {
                 *  Trace.WriteLine(e.Message);
                 *  throw;
                 * }
                 * Thread.Sleep(10000);
                 * }*/
                catch (OperationCanceledException e)
                {
                    if (!IsStopped)
                    {
                        Trace.WriteLine(e.Message);
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    ErrorSupport.ReportException(ex);
                    throw;
                }
            }
        }