title: Want to use Task Parallel Library with progress reporting for updating database
body: <p>i developed a application where multiple connection string are stored. i just iterate in for loop and connect each db and execute sql against each db. in this way i update multiple database with bulk sql statement. </p>
  
  <p>Now i need to use now Task Parallel Library to update multiple db simultaneously instead of updating one after one in loop. i also want to handle exception and also want to show multiple progress bar for multiple database operation. pause &amp; resume functionality should be there.</p>
  
  <p>when i will click on button then multiple db connection will be open and for each task a new progress bar will be added in my form. each progress bar will show each db operation progress. when any task will be finish then that corresponding progress bar will be removed from form.</p>
  
  <p>any one can guide me with sample code how to do it using TPL.
  here i got one code which update one progressbar but i need to update multiple progress bar.
  int iterations = 100;   </p>
  
  <pre><code>ProgressBar pb = new ProgressBar();   
  pb.Maximum = iterations;   
  pb.Dock = DockStyle.Fill;   
  Controls.Add(pb);   
  
  Task.Create(delegate   
  {   
      Parallel.For(0, iterations, i =&gt;  
      {   
          Thread.SpinWait(50000000); // do work here   
          BeginInvoke((Action)delegate { pb.Value++; });   
      });   
  }); 
  </code></pre>
  
  <h2>UPDATE Question</h2>
  
  <p>i did it in this way. code works but all progress bar value increase sequentially.
  i have one form and one user control in winform apps. please have a look at my code and tell me what is wrong there.</p>
  
  <h2>main for code</h2>
  
  <pre><code>public partial class Main : Form
      {
          public Main()
          {
              InitializeComponent();
              this.DoubleBuffered = true;
          }
  
          private void btnStart_Click(object sender, EventArgs e)
          {
              Progress ucProgress = null;
              Dictionary&lt;string, string&gt; dicList = new Dictionary&lt;string, string&gt;();
              dicList.Add("GB", "conn1");
              dicList.Add("US", "conn2");
              dicList.Add("DE", "conn3");
              fpPanel.Controls.Clear();
  
              Task.Factory.StartNew(() =&gt;
              {
                  foreach (KeyValuePair&lt;string, string&gt; entry in dicList)
                  {
                      ucProgress = new Progress();
                      ucProgress.Country = entry.Key;
                      ucProgress.DBConnection = entry.Value;
  
                      fpPanel.BeginInvoke((MethodInvoker)delegate
                      {
                          fpPanel.Controls.Add(ucProgress);
                          ucProgress.Process();
                      });
                      //fpPanel.Controls.Add(ucProgress);
  
  
                      System.Threading.Thread.SpinWait(5000000);
                  }
              });
  
          }
  
          private void Main_Resize(object sender, EventArgs e)
          {
              this.Invalidate();
          }
          private void Main_Paint(object sender, PaintEventArgs e)
          {
              using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle,
                                                                 Color.WhiteSmoke,
                                                                 Color.LightGray,
                                                                 90F))
              {
                  e.Graphics.FillRectangle(brush, this.ClientRectangle);
              }
          }
      }
  </code></pre>
  
  <h2>user control code</h2>
  
  <pre><code>public partial class Progress : UserControl
      {
          public Progress()
          {
              InitializeComponent();
              lblMsg.Text = "";
              pbStatus.Minimum = 0;
              pbStatus.Maximum = 100;
          }
  
          public string Country { get; set; }
          public string DBConnection { get; set; }
          public string Sql { get; set; }
  
          public void SetMessage(string strMsg)
          {
              lblMsg.Text = strMsg;
          }
  
          public void Process()
          {
              var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
              Task.Factory.StartNew(() =&gt;
              {
                  lblMsg.BeginInvoke((MethodInvoker)delegate
                  {
                      lblMsg.Text = "Connecting country " + Country;
                  });
  
                  pbStatus.BeginInvoke((MethodInvoker)delegate
                  {
                      pbStatus.Value = 30;
                  });
                  System.Threading.Thread.SpinWait(50000000);
  
                  //***********
                  lblMsg.BeginInvoke((MethodInvoker)delegate
                  {
                      lblMsg.Text = "executing sql for country " + Country;
                  });
  
                  pbStatus.BeginInvoke((MethodInvoker)delegate
                  {
                      pbStatus.Value = 60;
                  });
                  System.Threading.Thread.SpinWait(50000000);
  
                  //***********
  
                  lblMsg.BeginInvoke((MethodInvoker)delegate
                  {
                      lblMsg.Text = "sql executed successfully for country " + Country;
                  });
  
                  pbStatus.BeginInvoke((MethodInvoker)delegate
                  {
                      pbStatus.Value = 100;
                  });
                  System.Threading.Thread.SpinWait(50000000);
  
              });
              //System.Threading.Thread.SpinWait(50000000); // do work here   
          }
      }
  </code></pre>