Click here to Skip to main content
15,906,296 members
Home / Discussions / C#
   

C#

 
GeneralRe: visual studio 2010 c# error Pin
User349029-Mar-13 1:30
User349029-Mar-13 1:30 
GeneralRe: visual studio 2010 c# error Pin
annex4529-Mar-13 1:32
annex4529-Mar-13 1:32 
QuestionSimulating Mouse Scrolling Pin
annex4528-Mar-13 12:19
annex4528-Mar-13 12:19 
AnswerRe: Simulating Mouse Scrolling Pin
Jasmine250128-Mar-13 13:10
Jasmine250128-Mar-13 13:10 
QuestionCreate MP4 video from images Pin
cabasm28-Mar-13 10:35
cabasm28-Mar-13 10:35 
AnswerRe: Create MP4 video from images Pin
Gerry Schmitz28-Mar-13 12:08
mveGerry Schmitz28-Mar-13 12:08 
GeneralRe: Create MP4 video from images Pin
cabasm28-Mar-13 12:37
cabasm28-Mar-13 12:37 
GeneralRe: Create MP4 video from images Pin
Gerry Schmitz28-Mar-13 12:47
mveGerry Schmitz28-Mar-13 12:47 
GeneralRe: Create MP4 video from images Pin
cabasm29-Mar-13 4:46
cabasm29-Mar-13 4:46 
QuestionC# WPF Accessing parameter of chosen data grid row Pin
johnyjj228-Mar-13 8:47
johnyjj228-Mar-13 8:47 
AnswerRe: C# WPF Accessing parameter of chosen data grid row Pin
OriginalGriff28-Mar-13 9:20
mveOriginalGriff28-Mar-13 9:20 
QuestionPass Info from Desktop App to Webpage Pin
PDTUM28-Mar-13 7:44
PDTUM28-Mar-13 7:44 
AnswerRe: Pass Info from Desktop App to Webpage Pin
David C# Hobbyist.28-Mar-13 8:42
professionalDavid C# Hobbyist.28-Mar-13 8:42 
Questionprogramming ideas Pin
MegaBaitas28-Mar-13 3:45
MegaBaitas28-Mar-13 3:45 
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 

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.