-
Notifications
You must be signed in to change notification settings - Fork 616
Closed
Labels
Description
See:
rabbitmq-dotnet-client/projects/client/RabbitMQ.Client/src/client/api/DefaultBasicConsumer.cs
Line 104 in 13a9567
| public event EventHandler<ConsumerEventArgs> ConsumerCancelled |
and
rabbitmq-dotnet-client/projects/client/RabbitMQ.Client/src/client/api/DefaultBasicConsumer.cs
Line 194 in 13a9567
| public virtual void OnCancel() |
In C#, events are already thread safe. No lock protection is needed to subscribe to events, or to fire events. See section 15.8.2 "Field-like events" in the spec, https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-334.pdf
Prior to C#6, the code for the ConsumerCancelled event would be:
public event EventHandler<ConsumerEventArgs> ConsumerCancelled;
public virtual void OnCancel()
{
IsRunning = false;
var handler = ConsumerCancelled;
if( handler != null )
{
handler(this, new ConsumerEventArgs(ConsumerTag);
}
}
After C#6 (supported by Visual Studio 2015). you can further simplify the OnCancel() method with:
public virtual void OnCancel()
{
IsRunning = false;
ConsumerCancelled?.Invoke( this, new ConsumerEventArgs(ConsumerTag);
}