.net - Event Handlers in Constructors - Is it Possible or Even Wise? -


I have an object that can take from second to second to start up to two minutes. The reason for this is that the manufacturer receives data from a webservice, which can be for several megabytes of kilobytes, and may vary greatly depending on the speed of the user's connection, for this reason I include events. I want to handle the progress notification.

This is my question: Can I place event handlers in the controller or should this type of action be done with the loading method?

For example:

Public class MyObject {public event event handler & lt; UpdateLoadProgressEventArgs & gt; UpdateLoadProgress; Public MyObject (int id) {background worker bgWorker = new background player (); BgWorker.DoWork + = Representative (Objects, DoWorkEventArgs args) {// Load data and update updates as updated UpdateLoadProgress (this, New UpadteLoadProgressEventArgs (progressValue)); Result = some value; } BgWorker.RunWorkAsync (); } The result of public spacing {get; Set; }}

However, when I try to tie event handlers to the manufacturer, they are always empty when the call is made:

  MyObject O = new MyObject (1); O.UpdateLoadProgress + = New Event Handler & lt; EventArgs & gt; (O_UpdateLoadProgress);  

I think this is because I wire events after the producer. I see that the only option is to create a load method that works as a constructor. The downside is that whosoever uses this class should know that it is to call the load before using the result (or any other asset).

Edit: This is the last resort:

My Objectbuilder class

  public Class MyObjectBuilder {Changed EventHandler Progress Changing Public Event Progress; Public MyObject CreateMyObject () {MyObject o = New MyObject (); O.Load (ProgressChanged); Return O; }}  

MyObject class

  public class MyObject {public ET results {get; Set;} Public Zero Load (Progressive Endhandler Handler) {BackgroundWalker BigWalker = new background player (); BgWorker.WorkerReports Progress = true; BgWorker. Progress changed + = handler; BgWorker.DoWork + = Representative (Objects, DoWorkEventArgs args) {for (int i = 0; i & lt; 100; i ++) {Thread. Sleep (10); Results = i; BgWorker.ReportProgress (i); }}; BgWorker.RunWorkerAsync (); }}  

Program class

  class program {static zero main (string [] arg) {MyObjectBuilder builder = new MyObjectBuilder (); Builder. Changed progress + = new progress changing event handler (builder crib changed); MyObject O = Builder CreateMyObject (); Console.ReadLine (); } Fixed zero builder changes in progress (object sender, progress progress eventArgs e) {Console.WriteLine (e.ProgressPercentage); }}  

Another option event handlers must be passed in the constructor too.

Personally, I try to avoid doing something in a constructor like this. The creation of a new object should not normally start the background function, you can put it in a static method instead of the IMO - which can call a personal constructor.

You can also divide your class into two - a builder who is ready for everything (for example the event), and then an "in-flight or full" class, in which Results are assets you can call start or similar to first class to get an example of another.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -