public void StartCollectingEvents(Domain propogationDomain) { Task.Factory.StartNew( () => { while (true) { RaisedEvent @event; if (buffer.TryDequeue(out @event)) { propogationDomain.Consume(@event); } else { //We empty the queue before stopping as this queue is in-memory only. if (cancellationTokenSource.Token.IsCancellationRequested) return; nudge.WaitOne(); } } }) .ContinueWith(task => stoppedSignal.Set()); }
public void StartCollectingEvents(Domain propogationDomain) { Task.Factory.StartNew( () => { while (true) { BrokeredMessage message; try { message = receiver.Receive(receiveWaitTime); if (message == null) { Trace.TraceInformation("No message waiting"); continue; } } catch (OperationCanceledException) { Trace.TraceInformation("Stopping receiver"); stopped.Set(); return; } catch (Exception exception) { if (exception is UnauthorizedAccessException | exception is CommunicationException | exception is MessagingException | exception is TimeoutException | exception is MessagingCommunicationException | exception is ServerBusyException) { reopenReceiver(receiver, exception, receiverCancellationTokenSource.Token); continue; // Receiver loop. } Trace.TraceError("Stopping event receiver which died with exception:\n{0}", exception); stopped.Set(); throw; } var raisedEvent = message.ConvertToEvent(); propogationDomain.Consume(raisedEvent); message.Complete(); } }, TaskCreationOptions.LongRunning); }