private void RunTask(IProgress <EstimatorBaseProgress> progress) { ProgressLogTask(progress, "Task is started."); while (true) { ProgressStatusTask(progress, ProgressTaskStatus.Working); ProgressIterationTask(progress, true); ProgressLogTask(progress, "Iteration is started."); WaitTask(); IList <LanguageMapping> fromeEnglishMappings = _excelService.GetFromEnglishMappings().ToList(); IList <LanguageMapping> toEnglishMappings = _excelService.GetToEnglishMappings().ToList(); ProgressLogTask(progress, "Excel settings are read correctly."); WaitTask(); IList <EmailMessageItem> messages = _emailExchangeService.GetUnreadMessages().ToList(); ProgressInfoTask(progress, null, messages.Count, null, null); ProgressLogTask(progress, String.Format("Found {0} emails.", messages.Count)); WaitTask(); foreach (EmailMessageItem message in messages) { try { SalesEstimator model = EmailMessageItemProcessed(message, fromeEnglishMappings, toEnglishMappings, progress); _emailExchangeService.Reply(message, model); _emailExchangeService.MarkAsRead(message); ProgressInfoTask(progress, 1, null, message.Subject, null); ProgressLogTask(progress, String.Format("Emails with Subject: '{0}' processed success.", message.Subject)); WaitTask(); } catch (OperationCanceledException) { throw; } catch (ApplicationException ex) { ProgressLogTask(progress, ex.Message, ProgressType.Error); _emailExchangeService.ReplyWithException(message, ex); } catch (Exception exception) { ProgressLogTask(progress, exception.Message, ProgressType.Error); _emailExchangeService.ResendExceptionToAdmin(message, exception); } } if (messages.Any()) { ProgressLogTask(progress, "All emails processed."); WaitTask(); } ProgressStatusTask(progress, ProgressTaskStatus.Ended); ProgressIterationTask(progress, false); Int32 seconds = _settingsService.TaskPauseSeconds; ProgressLogTask(progress, String.Format("Waiting {0} seconds to next iteration.", seconds)); WaitTask(seconds * 1000); } }