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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:14:52+00:00 2026-05-16T04:14:52+00:00

I have an object that can take anywhere from a split second to a

  • 0

I have an object that can take anywhere from a split second to a couple of minutes to initialize. The reason being is that the constructor retrieves data from a webservice that could be a few kilobytes to several megabytes, and depending on the user’s connection speed performance may vary greatly. For that reason I am wanting to put events in that will handle progress notification.

Here is my question: Can I put event
handlers in the constructor or should
this type of action be done with a
Load method?

For example:

public class MyObject
{    
 public event EventHandler<UpdateLoadProgressEventArgs> UpdateLoadProgress;    

 public MyObject(int id)
 {
   Background worker bgWorker = new BackgroundWorker();
   bgWorker.DoWork += delegate(object s, DoWorkEventArgs args)
   {
        //load data and update progress incrementally
        UpdateLoadProgress(this, new UpadteLoadProgressEventArgs(progressValue));

        Result = someValue;         
   }
   bgWorker.RunWorkAsync();

 } 

 public int Result
 {
  get;
  set;
 }

} 

However when I try to tie the event handlers to the constructor they are always null when being called:

MyObject o = new MyObject(1);
o.UpdateLoadProgress += new EventHandler<EventArgs>(o_UpdateLoadProgress);

I assume this happens because I wire up the events after the constructor. The only alternative I see is creating a Load method that does the work of the constructor. The downside is that anyone that uses this class must know to call Load before trying to access Result (or any other property).

EDIT:
Here is the final solution:

MyObjectBuilder Class

public class MyObjectBuilder
    {
        public event ProgressChangedEventHandler ProgressChanged;

        public MyObject CreateMyObject()
        {
            MyObject o = new MyObject();
            o.Load(ProgressChanged);

            return o;
        }
    }

MyObject Class

public class MyObject 
    {
        public int Result { get; set;}

        public void Load(ProgressChangedEventHandler handler)
        {
            BackgroundWorker bgWorker = new BackgroundWorker();
            bgWorker.WorkerReportsProgress = true;
            bgWorker.ProgressChanged += handler;

            bgWorker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                for (int i = 0; i < 100; i++)
                {
                    Thread.Sleep(10);
                    Result = i;

                    bgWorker.ReportProgress(i);
                }
            };
            bgWorker.RunWorkerAsync();                      
        }
    }

Program Class

class Program
    {
        static void Main(string[] args)
        {
            MyObjectBuilder builder = new MyObjectBuilder();
            builder.ProgressChanged += new ProgressChangedEventHandler(builder_ProgressChanged);        

            MyObject o = builder.CreateMyObject();
            Console.ReadLine();
        }

        static void builder_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Console.WriteLine(e.ProgressPercentage);
        }
    }
  • 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-16T04:14:52+00:00Added an answer on May 16, 2026 at 4:14 am

    Another option would be to pass the event handlers into the constructor too, of course.

    Personally I try to avoid doing something like this within a constructor. Creating a new object shouldn’t generally start background tasks, IMO. You may want to put it into a static method instead – which can call a private constructor, of course.

    You could also split up your class into two – a builder which gets everything ready (e.g. the events), and then an “in-flight or completed” class, which has the Result property. You’d call Start or something similar on the first class to get an instance of the second.

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

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have an app that has a tabBar Controller and a navBar Controller. It
I have a VB6 ActiveX DLL that has run fine on all our machines
The JAXB documentation is like a text-book, and I simply don't have to time
scenario: you need to update 2 fields of a customer you don't have a
i have a list of say courses and certificates and fun_days . These are
I'm developing a XAML system which at present involves an icon in the taskbar
First things first, here is a little snippet code to help explain my problem:
I'm running Sql Server Management Studio 2008 on a decent machine. Even if it
I'm a bit confused about handling an array of objects in C++, as 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.