private void WorkToDo() { // while no cancel request made while (!_isCanceled) { // do work try { EventDto[] events = _msmqProvider.Receive(TimeSpan.FromSeconds(1)); //must change them into Activity Events _adapter.PropagateEvents(events.Select(x => x.ToActivityEvent()).ToArray()); } catch (MessageQueueException exception) { switch (exception.MessageQueueErrorCode) { // All OK, just timed out, // we need this to see if cancel is set to true // If we don't timeout we can keep reading from queue // which can locks if nothing goes in case MessageQueueErrorCode.IOTimeout: break; // Something bad happened with msmq, throw exception default: throw; } } catch (Exception ex) { _exLog.Error(ex); } } _log.Debug("MSMQ producer thread complied with cancel request"); }