Click here to Skip to main content
15,891,204 members
Home / Discussions / C#
   

C#

 
AnswerRe: programming ideas Pin
Richard MacCutchan28-Mar-13 3:58
mveRichard MacCutchan28-Mar-13 3:58 
AnswerRe: programming ideas Pin
PIEBALDconsult28-Mar-13 4:01
mvePIEBALDconsult28-Mar-13 4:01 
Generalprogramming ideas Pin
MegaBaitas28-Mar-13 5:11
MegaBaitas28-Mar-13 5:11 
GeneralRe: programming ideas Pin
PIEBALDconsult28-Mar-13 10:14
mvePIEBALDconsult28-Mar-13 10:14 
AnswerRe: programming ideas Pin
Alan Balkany1-Apr-13 5:08
Alan Balkany1-Apr-13 5:08 
QuestionBoost parralel messaging performance (TPL / Async) Pin
KillerX12328-Mar-13 2:29
KillerX12328-Mar-13 2:29 
QuestionMy issue is regarding handling multiple backgroud worker and multiple progressbar Pin
Tridip Bhattacharjee28-Mar-13 0:31
professionalTridip Bhattacharjee28-Mar-13 0:31 
AnswerRe: My issue is regarding handling multiple backgroud worker and multiple progressbar Pin
Thomas Duwe28-Mar-13 5:26
Thomas Duwe28-Mar-13 5:26 
Hi,
I updated your code and it should work, but I didn't test it, so you have to try it out.

C#
public partial class Form1 : Form
{
   static int pbCounter = 1;
   int counter = 0;

   public Form1()
   {
      InitializeComponent();
   }

   private void btnStart_Click(object sender, EventArgs e)
   {
      ProgressBar pb = new ProgressBar();

      if (pbCounter <= 10)
      {
         // assign counter to Tag property to determine later, which progressbar needs to be updated
         pb.Tag = counter++;

         pb.Width = txtUrl.Width;
         flowLayoutPanel1.Controls.Add(pb);
         pbCounter++;

         System.ComponentModel.BackgroundWorker backgroundWorker1 = new System.ComponentModel.BackgroundWorker();

         backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
         backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
         backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

         // don't forget to tell the backgroundworker,
         // that it can report the progress, otherwise nothing happens
         backgroundWorker1.WorkerReportsProgress = true;

         // give the backgroundworker as argument the current counter
         backgroundWorker1.RunWorkerAsync(counter);
      }
   }

   private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
   {
      // Get the BackgroundWorker that raised this event.
      System.ComponentModel.BackgroundWorker worker = sender as System.ComponentModel.BackgroundWorker;

      // do downloading and report progress with:
      // and replace the <percentToReport> with the percentage to report
      worker.ReportProgress(<percentToReport>, e.Argument);

      // after all is done:
      e.Result = e.Argument;
   }

   // This event handler deals with the results of the
   // background operation.
   private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
   {
      // First, handle the case where an exception was thrown.
      if (e.Error != null)
      {
         MessageBox.Show(e.Error.Message);
      }
      else if (e.Cancelled)
      {
         //# "Canceled";
      }
      else
      {
         pbCounter--;

         ProgressBar barToRemove = null;

         foreach (object control in flowLayoutPanel1.Controls)
         {
            ProgressBar bar = control as ProgressBar;

            // if control is ProgressBar and the Result contains the
            // same number as the progressbar instance's Tag property
            // we found the correct progressbar to remove
            if (bar != null && bar.Tag == e.Result)
            {
               barToRemove = bar;

               break;
            }
         }

         if (barToRemove != null)
         {
            flowLayoutPanel1.Controls.Remove(barToRemove);
         }

         // Finally, handle the case where the operation  
         // succeeded.
         //#resultLabel.Text = e.Result.ToString();
      }
   }

   // This event handler updates the progress bar.
   private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
   {
      foreach(object control in flowLayoutPanel1.Controls)
      {
         ProgressBar bar = control as ProgressBar;

         // if control is ProgressBar and the UserState contains the
         // same number as the progressbar instance's Tag property
         // we found the correct progressbar to update
         if (bar != null && bar.Tag == e.UserState)
         {
            bar.Value = e.ProgressPercentage;

            break;
         }
      }
   }
}



Hope this helps,

Thomas.
GeneralRe: My issue is regarding handling multiple backgroud worker and multiple progressbar Pin
Tridip Bhattacharjee30-Mar-13 5:35
professionalTridip Bhattacharjee30-Mar-13 5:35 
GeneralRe: My issue is regarding handling multiple backgroud worker and multiple progressbar Pin
Thomas Duwe1-Apr-13 5:37
Thomas Duwe1-Apr-13 5:37 
QuestionHttpWebRequest and strange networking errors Pin
cjb11028-Mar-13 0:10
cjb11028-Mar-13 0:10 
QuestionHow to enlarge the recording voice Pin
Star.jon27-Mar-13 22:46
Star.jon27-Mar-13 22:46 
AnswerRe: How to enlarge the recording voice Pin
Bernhard Hiller27-Mar-13 23:01
Bernhard Hiller27-Mar-13 23:01 
GeneralRe: How to enlarge the recording voice Pin
Star.jon27-Mar-13 23:05
Star.jon27-Mar-13 23:05 
GeneralRe: How to enlarge the recording voice Pin
Dave Kreskowiak28-Mar-13 2:00
mveDave Kreskowiak28-Mar-13 2:00 
Questionerror in crystal reports Pin
User349027-Mar-13 13:36
User349027-Mar-13 13:36 
AnswerRe: error in crystal reports Pin
Abhinav S27-Mar-13 17:58
Abhinav S27-Mar-13 17:58 
AnswerRe: error in crystal reports Pin
V.27-Mar-13 22:21
professionalV.27-Mar-13 22:21 
GeneralRe: error in crystal reports Pin
User349028-Mar-13 7:49
User349028-Mar-13 7:49 
GeneralRe: error in crystal reports Pin
V.28-Mar-13 8:19
professionalV.28-Mar-13 8:19 
QuestionHow to Async download multiple files using webclient simultaneously? Pin
Tridip Bhattacharjee27-Mar-13 8:01
professionalTridip Bhattacharjee27-Mar-13 8:01 
QuestionScreenshots -> Video file Pin
ADASD33333227-Mar-13 4:06
ADASD33333227-Mar-13 4:06 
AnswerRe: Screenshots -> Video file Pin
Abhinav S29-Mar-13 4:00
Abhinav S29-Mar-13 4:00 
QuestionSaving Screensaver in C# Pin
greylakota27-Mar-13 3:34
greylakota27-Mar-13 3:34 
QuestionRe: Saving Screensaver in C# Pin
Richard MacCutchan27-Mar-13 3:56
mveRichard MacCutchan27-Mar-13 3:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.