Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal sealed class Listener
- {
- private readonly CancellationTokenSource cancellationTokenSource;
- public Listener()
- {
- this.cancellationTokenSource = new CancellationTokenSource();
- }
- public void Start()
- {
- Task.Factory.StartNew(LongRunningTask, cancellationTokenSource.Token);
- }
- private void LongRunningTask()
- {
- using(var connection = new... )
- using(var channel = connection.CreateChannel())
- {
- // EventingBasicConsumer is part of RMQ
- var consumer = new EventingBasicConsumer(channel);
- consumer.Received += Consumer_Received;
- while(!cancellationTokenSource.Token.IsCancellationRequested)
- {
- // to avoid cpu hogging
- Thread.Sleep(1);
- }
- }
- }
- private void Consumer_Received(object sender, BasicDeliveryArgs e)
- {
- OnMessageReceived(this, new MessageReceivedEventArgs()
- {
- // set some properties
- });
- }
- }
Add Comment
Please, Sign In to add comment