Ben Lesh
Ben Lesh

Reputation: 108471

In C#, how can I halt the execution of an event from one of its bound delegates?

I am currently using an event to fire off a set of delegates, and I need to be able to prevent the remaining delegates from firing from within another delegate.

The following code replicates the scenario...

//nonsense class
public class Foo {
    public string Bar { get; set; }
}

//nonsense event.
public event Action<Foo> SomeEvent;

void BindAndFire() {
    //wire it up.
    SomeEvent += (foo) => { 
        var a = DateTime.Now.Millisecond % 2;
        foo.Bar = (a == 0) ? "Hooray" : "Boo";
    };
    SomeEvent += (foo) => { 
        if(foo.Bar == "Boo") {
            SomeEvent.CANCEL_THE_REST(); //what do I do here?
        }
    };
    SomeEvent += (foo) => { 
        foo.Bar += ", you made it!";
    };

    //fire the event.
    SomeEvent(new Foo());
}

Given the above, what do I need to do? I'm 100% okay with changing to a List of Actions or something, but it seems like I should be able to stop the execution of subsequent events.

NOTE: The code shown is just to duplicate the issue, and is NOT my program structure. The actual program is processing incoming network events and has a list of arbitrary delegates it runs on them... I didn't want to post that whole thing for one issue.

... maybe I'm just trying to use them in a way they weren't intended to be used, I'm I'm fine with that, I'm just hoping I only need to learn some new thing about events in C#.

Thank you for your time!

Upvotes: 3

Views: 335

Answers (2)

Ben Lesh
Ben Lesh

Reputation: 108471

I think I may have answered my own question.

What I ended up doing is having my delegates return a boolean, and then when I go to execute my event, I actually use GetInvocationList() to get the delegates and call them, checking the return value...

The new code looks like so:

//nonsense class
public class Foo {
    public string Bar { get; set; }
}

//nonsense event.
public event Func<Foo, bool> SomeEvent;

void BindAndFire() {
    //wire it up.
    SomeEvent += (foo) => { 
        var a = DateTime.Now.Millisecond % 2;
        foo.Bar = (a == 0) ? "Hooray" : "Boo";
        return true;
    };
    SomeEvent += (foo) => { 
        return foo.Bar != "Boo";
    };
    SomeEvent += (foo) => { 
        foo.Bar += ", you made it!";
        return true;
    };

    //fire the event.
    var f = new Foo();
    foreach(var s in SomeEvent.GetInvocationList()) {
        if(!s.DynamicInvoke(f)) break;
    }
}

EDIT: I ended up using a combination of my answer and Blueberry's above, because it suited what I was working on better. So in my case, one of the event arguments had a Close() method that the develop might call inside of his delegate, so in there, I was setting a property which I would then check and break on while looping through GetInvocationList().

//nonsense class
public class Foo {
    public string Bar { get; set; }
    public bool IsClosed { get; private set; }
    public void Close() {
        IsClosed = true;
    }
}

//nonsense event.
public event Action<Foo> SomeEvent;

void BindAndFire() {
    //wire it up.
    SomeEvent += (foo) => { 
        var a = DateTime.Now.Millisecond % 2;
        foo.Bar = (a == 0) ? "Hooray" : "Boo";
    };
    SomeEvent += (foo) => { 
        if(foo.Bar != "Boo") {
           foo.Close();
        }
    };
    SomeEvent += (foo) => { 
        foo.Bar += ", you made it!";
    };

    //fire the event.
    var f = new Foo();
    foreach(var s in SomeEvent.GetInvocationList()) {
        s.DynamicInvoke(f);
        if(f.IsClosed) break;
    }
}

Upvotes: 1

Blueberry
Blueberry

Reputation: 2231

You could use something like HandledEventArgs (or use a similar concept), with this, when the event is handled, you set the 'handled' property and check this property in your other delegates.

Not sure you can force it to cancel all other calls, and may get a little messy to debug if you did later on.

Upvotes: 2

Related Questions