Help me to implement an event, which handler can cancel it.
public class BuildStartEventArgs : EventArgs
{
    public bool Cancel { get; set; }
}
class Foo
{
    public event EventHandler<BuildStartEventArgs> BuildStart;
    private void Bar()
    {
        // build started
        OnBuildStart(new BuildStartEventArgs());
        // how to catch cancellation?
    }
    private void OnBuildStart(BuildStartEventArgs e)
    {
        if (this.BuildStart != null)
        {
            this.BuildStart(this, e);
        }
    }
}
                You need to modify this code:
private void Bar()
{
    // build started
    OnBuildStart(new BuildStartEventArgs());
    // how to catch cancellation?
}
to something like this:
private void Bar()
{
    var e = new BuildStartEventArgs();
    OnBuildStart(e);
    if (!e.Cancel) {
      // Do build
    }
}
Classes in .NET have reference semantics, so you can see any changes made to the object the parameter of the event references.
Your BuildStartEventArgs are redundant, the framework already offers the CancelEventArgs class – consider using it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With