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 5967869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:00:24+00:00 2026-05-22T20:00:24+00:00

I need to start a download of some html so I call void GetHTML

  • 0

I need to start a download of some html so I call void GetHTML in another class. When it’s done I want to pass along what Event it should raise in the calling class. How could I do this?

So it would look something like this:

public class Stuff
{
    public void GetHTML(string url, event to raise here)
    {
       //Do stuff then raise event
    }
}
public class Other
{
    public Other()
    {
        Stuff stuff = new Stuff();
        stuff.GetHTML(someUrl, somehow sending info that HTML_Done should be called);
    }
    void HTML_Done(string result, Event e)
    {
         //Do stuff with the result since it's done
    }
}

I realize I’m not super clear with what I want to do, I’d be glad to fill in any missing parts.

Thanks for any suggestions!

  • 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-05-22T20:00:25+00:00Added an answer on May 22, 2026 at 8:00 pm

    Event Subscription and Notification

    public class Stuff
    {
        // Public Event to allow other classes to subscribe to.
        public event EventHandler GetHtmlDone = delegate { };
    
        public void GetHTML(string url)
        {
            //Do stuff
    
            // Raise Event, which triggers all method subscribed to it!
            this.GetHtmlDone(this, new EventArgs());
        }
    }
    
    public class Other
    {
        public Other()
        {
            Stuff stuff = new Stuff();
    
            // Subscribe to the event.
            stuff.GetHtmlDone += new EventHandler(OnGetHtmlDone);
    
            // Execute
            stuff.GetHTML("someUrl");
        }
    
        void OnGetHtmlDone(object sender, EventArgs e)
        {
            //Do stuff with the result since it's done
        }
    }
    

    Using this pattern allows many more subscribers.

    You also do not tie the notifier, the Stuff class, to the caller, the Other class.

    You either have subscribers or not, no difference to the Stuff class.

    The Stuff class should not know about the subscriber, it should merely raise an event it exposes for subscription.

    EDIT
    As ctacke pointed out correctly in the comments, raising the event using this.GetHtmlDone(this, new EventArgs()); will cause an exception if nobody has subscribed.
    I changed my code above to ensure the event can be raised safely at all times by initialising my eventhandler.
    As I’m always using it (by raising it) I’m sure it is only good practise to always initialise what you are using.

    I could have added a null check on the event handler but in my personal oppinion I do not agree with that having to be the responsibility of the stuffclass. I feel the event should always be raised as it is the “responsible” thing to do.

    I found this thread on SO which kind of confirmed to me that it does not seem wrong to do so.

    In addition I also run Code Analysis on that code to ensure I’m not breaking the CA1805 rule by initialising the EventHandler. CA1805 was not raised and no rules have been broken.

    Using my car analogy from the comments, I believe not initializing the eventhandler and raising it all the time would be the same than saying “When turning a corner in your car only use your indicator if someone is watching and if not don’t bother”. You never know if anyone is watching, so you might as well make sure you always do it.

    This is simply my personal preference. Anyone else, please do add the != null check at all times if that is how you prefere to do it.

    Thank you so much for the comments ctacke and for pointing this out. I learned quite a lot from this.
    I will have to go back to some of my projects now and update some code to make sure my libraries are not crashing if nobody subscribes to my events. I feel quite silly not ever having caught that in any of my Tests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create windows service that would: download some files from uri (google.com,
I need to create windows service that would: download some files from uri (google.com,
I need to download some images from the Internet and right now I'm using
I need to download some data and parse the results at some intervals. Right
I need start off with code because I am not sure what terminology to
Normally to attach a debuger to a running jvm you would need start the
I need to start messing with a vb.net(vs2008) project file and I'd like a
I need to start and stop SQL Server from the command line. I am
I need to start developing in FORTRAN, any recommendations on learning\developing FORTRAN coming from
I need Week-Start-Date and Week-End-Date for particular Week number in a year (vb.net or

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.