Guest User

Untitled

a guest
Jul 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. internal sealed class Listener
  2. {
  3. private readonly CancellationTokenSource cancellationTokenSource;
  4.  
  5. public Listener()
  6. {
  7. this.cancellationTokenSource = new CancellationTokenSource();
  8. }
  9.  
  10. public void Start()
  11. {
  12. Task.Factory.StartNew(LongRunningTask, cancellationTokenSource.Token);
  13. }
  14.  
  15. private void LongRunningTask()
  16. {
  17.  
  18. using(var connection = new... )
  19. using(var channel = connection.CreateChannel())
  20. {
  21. // EventingBasicConsumer is part of RMQ
  22. var consumer = new EventingBasicConsumer(channel);
  23. consumer.Received += Consumer_Received;
  24.  
  25. while(!cancellationTokenSource.Token.IsCancellationRequested)
  26. {
  27. // to avoid cpu hogging
  28. Thread.Sleep(1);
  29. }
  30. }
  31. }
  32.  
  33. private void Consumer_Received(object sender, BasicDeliveryArgs e)
  34. {
  35. OnMessageReceived(this, new MessageReceivedEventArgs()
  36. {
  37. // set some properties
  38. });
  39. }
  40. }
Add Comment
Please, Sign In to add comment