Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8128153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:47:06+00:00 2026-06-06T07:47:06+00:00

I am currently using an event to fire off a set of delegates, and

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:47:09+00:00Added an answer on June 6, 2026 at 7:47 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently using the change function from jQuery. The change event is only fired
Currently I am using a post-build event command line similar to this one: xcopy
I am currently using PIL. from PIL import Image try: im = Image.open(filename) #
I just want to fire an event when an input changes value using jQuery
We are using WatiN from C# to interact with a browser (currently only IE).
(WindowClosing) when i am using window event for closing current frame, in that it
Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted
Currently using Google Analytics as a supplement to our paid tracking software, but neither
Currently using MySQL version 5.1.6 This is my first real world build and so
Currently using Xcode 4.2 and I have two view controllers (1 and 2). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.