Skip to content

Improve event publishing in DefaultBasicConsumer #512

@antiduh

Description

@antiduh

See:

public event EventHandler<ConsumerEventArgs> ConsumerCancelled

and

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);
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions