On Tuesday, 29 August 2017 at 05:10:25 UTC, bitwise wrote:
[...]

I think I should clarify for anyone with limited C# experience, that I'm talking about the custom-event syntax, not the regular one-liner syntax:

class MyClass
{
    Object myLock;
    EventHandler _completed;

    public event EventHandler Completed
    {
        add {
            lock (myLock) {
_completed = (EventHandler)Delegate.Combine(_completed, value);
                // update some other state
            }
        }
        remove {
            lock(myLock) {
_completed = (EventHandler)Delegate.Remove(_completed, value);
                // update some other state
            }
        }
    }

    void RaiseCompleted()
    {
        EventHandler c = null;

        lock(myLock) {
            c = _completed;
        }

        if(c != null)
            c();
    }
}


Reply via email to