public void Run() { try { do { SetExecutingCommand(true); CurrentTicket = Tickets.Take(); Console.WriteLine("TICKET ATUAL SERIAL " + CurrentTicket.Ticket.Serial + " COMANDO " + CurrentTicket.Ticket.CommandType); foreach (var command in CurrentTicket.Ticket.CommandTickets) { CurrentCommand = command; Console.WriteLine("COMANDO ATUAL " + command.CommandId); CommunicationStatus = command.CommunicationStatus; Status = command.Status; var forwarderMessage = CurrentTicket.ForwarderMessages.Find(message => message.CommandId == command.CommandId); var tries = 0; var stillWaitingForResponse = false; do { tries++; Console.WriteLine("TENTATIVA " + tries); var sent = SendCommand(forwarderMessage); if (sent.IsSuccessStatusCode) { SetWaitingCommandMessage(true); Console.WriteLine("AGUARDANDO RESPOSTA"); stillWaitingForResponse = WaitConfirmation(); if (!stillWaitingForResponse) { break; } SetWaitingCommandMessage(false); } else { CurrentTicket.Ticket.TicketStatus = CurrentTicket.Ticket.TicketStatus == TicketStatus.Waiting ? TicketStatus.FailedToStart : TicketStatus.FailedToContinue; break; } Task.Delay(TimeSpan.FromSeconds(_commandRules.IntervalBetweenCommandTriesInSeconds)); } while (tries < _commandRules.NumberOfCommandTries); if (CurrentTicket.Ticket.TicketStatus == TicketStatus.FailedToStart || CurrentTicket.Ticket.TicketStatus == TicketStatus.FailedToContinue) { break; } if (stillWaitingForResponse) { continue; } Console.WriteLine("AGUARDANDO COMEÇAR"); if (CurrentCommand.Status == Status.Waiting) { WaitCommandStart(); } if (CurrentCommand.Status == Status.Waiting) { break; } Console.WriteLine("AGUARDANDO FINALIZAR"); if (CurrentCommand.Status == Status.Executing) { WaitCommandFinish(); } if (CurrentCommand.Status == Status.Executing) { break; } } SetExecutingCommand(false); _ticketsExecuted.Add(CurrentTicket); } while (AnyCommandExecutingOrWaiting()); _eventService.AThreadIsShuttingDownEvent(this, new IoGServicedEventArgs <ITicketThread>(this)); } catch (Exception e) { Console.WriteLine(e.Message); } }