开发者

Event handler/raiser code snippet

I would like to hear opinions on below code snippet. Is there anything that can be improved? Is the event handler/raiser naming following best practices? I know it is n开发者_运维技巧ot that useful to handle and raise events in the same class but this is just a snippet.

public class MyControl
{
    public MyControl()
    {
        this.LogWritten += this.HandleMyControlLogWritten;
    }

    // Event handler
    void HandleMyControlLogWritten(object sender, EventArgs e)
    {
    }

    // Event object
    public event Action<object, EventArgs> LogWritten;

    // Event raiser
    protected virtual void OnLogWritten(EventArgs e)
    {
        if (this.LogWritten != null)
        {
            this.LogWritten(this, e);
        }
    }
}


The main change I'd recommend would be to get a copy of the event handler:

// Event raiser
protected virtual void OnLogWritten(EventArgs e)
{
    var handler = this.LogWritten;
    if (handler != null)
    {
        handler(this, e);
    }
}

This is important if you're planning to (eventually) use this class in a multi-threaded scenario. As such, I find that it's a good "best practice" to get into the habit of using. The issue is that, when using in multiple threads, without creating the copy, it's possible that the only "handler" attached could unsubscribe between the null check and the invocation, which would cause a runtime error. By copying to a temporary variable (the var handler = this.LogWritten;) line, you're effectively creating a "snapshot" of the subscriber list, and then checking it for null and invoking if required.

The other change is in the event declaration itself. Instead of using Action<T1,T2>:

// Event object
public event Action<object, EventArgs> LogWritten;

I would recommend using EventHandler<TEventArgs> (if you want to use a custom EventArgs subclass) or EventHandler (for standard EventArgs). These are more "standard practice", and will be what other developers expect:

// Event object
public event EventHandler LogWritten;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜