c# - Background Worker Running Twice on bw_DoWork() and bw_ProgressChanged() when using textBox1.AppendText() -


i'm using visual studio 2013 , can't figure out why bw_progresschanged() runs twice when use textbox1.appendtext(). bw_dowork() runs twice.

the code copy paste microsoft example difference i'm using textbox1.appendtext() instead of textbox1.text = "".

the code looks this:

public partial class form1 : form {     public form1()     {         initializecomponent();          bw.workerreportsprogress = true;         bw.workersupportscancellation = true;         bw.dowork += new doworkeventhandler(bw_dowork);         bw.progresschanged += new progresschangedeventhandler(bw_progresschanged);         bw.runworkercompleted += new runworkercompletedeventhandler(bw_runworkercompleted);     }      private void button1_click(object sender, eventargs e)     {         if (bw.isbusy != true)         {             bw.runworkerasync();         }     }      private void bw_dowork(object sender, doworkeventargs e)     {         backgroundworker worker = sender backgroundworker;          (int = 1; (i <= 10); i++)         {             if ((worker.cancellationpending == true))             {                 e.cancel = true;                 break;             }             else             {                 // perform time consuming operation , report progress.                 system.threading.thread.sleep(500);                 worker.reportprogress((i * 10));             }         }     }      private void bw_progresschanged(object sender, progresschangedeventargs e)     {         this.textbox1.appendtext(e.progresspercentage.tostring() + "%\n");     }      private void bw_runworkercompleted(object sender, runworkercompletedeventargs e)     {         if ((e.cancelled == true))         {             this.textbox1.appendtext("canceled!");         }          else if (!(e.error == null))         {             this.textbox1.appendtext("error: " + e.error.message);         }          else         {             this.textbox1.appendtext("done!");         }     } } 

the textbox1 text looks this:

10% 10% 20% 20% 30% 30% 40% 40% 50% 50% 60% 60% 70% 70% 80% 80% 90% 90% 100% 100% 10% 10% 20% 20% 30% 30% 40% 40% 50% 50% 60% 60% 70% 70% 80% 80% 90% 90% 100% 100% done!done! 

instead of:

10% 20% 30% 40% 50% 60% 70% 80% 90% 100% done! 

i have tried pass progress directly local variable , pass textbox1 doesn't make difference. ideas why happening?

the code inside event method runs twice because you've somehow subscribed progresschanged event twice.

since don't see 2 subscriptions in code, you've subscribed through designer (check designer.cs file confirm), remove line constructor:

bw.progresschanged += new progresschangedeventhandler(bw_progresschanged); 

while you're @ it, double-check dowork , runworkercompleted too. you're subscribing twice well, explain why output printing 10 through 100, , printing same doubled-up values again.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -