c# - Subscribing to an event of a class that holds the override method of the class you reference -


i working background workers update progress bar in wpf ui working on. background worker getting progress updates multiple events subscribed to, because progress bar goes through several loading stages, , percentages come several places. here example/pseudo code explaining mean

the dowork method of background worker , methods using progress updates

// these working fine private void bwondowork(object sender, doworkeventargs doworkeventargs) {     orderprocessing.onorderprogress += orderstatus;     orderprocessing.onstandardorderprogress += standardorderstatus;     orderprocessing.createorders(orders);  }  private void orderstatus(int currentcount, int totalitems, string message) {    if (totalitems > 0)      bw.reportprogress(convert.toint32(((double)currentcount / (double)totalitems) * 100),                         message); }  private void standardorderstatus(int currentcount, int totalitems, string message) {    if (totalitems > 0)       bw.reportprogress(convert.toint32(((double)currentcount / (double)totalitems) * 100),                         message); } 

some code order processing class

public abstract class orderprocessing {    public delegate void orderprogress(int currentitems, int totalitems, string message);    public event mastersalesorder.standardorderprogress onstandardorderprogress;    public event orderprogress onorderprogress;     public abstract list<mastersalesorder> createorders(list<order> orders); } 

some code class holds override method createorders()

public abstract class orderprocessingfile : orderprocessing {     public event orderprogress onorderprogress;      public override list<mastersalesorder> createorders(list<order> orders)     {         //does stuff          foreach(var stuff in stuffs)         {             onorderprogress(currentcount, totalcount, "message");          }     } } 

since not explaining well, need info orderprocessingfiles onorderprogress event via orderprocessing class create in dowork method.i unsure on how subscribe event when code never directly instantiates instance of orderprocessingfile class , never directly referred to.

i have tried looking answers title show having hard time wording in way useful results, , genuinely stuck on one. let me know if more detail needed, tried strip down code relevant parts feel i'm explaining strangely.

i recommend create thread safe singleton progress manager. have each of background workers contact updates. progress manager use dispatchertimer (which runs on gui thread) update gui appropriately.

raw example:

public static class statusreportmanager {    // standard singleton code create manager , access it.    // start/create dispatch time well.     private static dispatchertimer timer { get; set; }      private static object _syncobject = new object();     public static void reportstatus(...)     {       lock (_syncobject)       {          // process states , set instance properties reading          // timer operation.           }            }     private void showstatus() // used dispatch timer    {         lock (_syncobject)         {            // updates gui in here current state.         }    } } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -