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!
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:
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().