title: VB.Net Asynchronous background worker inside a loop
body: <p>Im using Vb.net visual studio 2008. i have got a process(system.diagnostics.process) to be run in background and it updates the Ui thread progressbar and a label and i have worked it using backgroundworker.runworkerAsync. Now the problem is i have to work the same process for a number of time with different inputs.</p>
  
  <p>The code block is :</p>
  
  <pre><code>Private Sub fnStartEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.click
  
  Dim inputFolder = Directory.GetFiles(textboxSourceFolder.text) 
  Dim currentNumberOfFile As Integer=1
  For each Files in inputFolder
     Dim arguments As Object = Files
     updateProgressStatus(0, currentNumberOfFile)
     BackgroundWorker1.WorkerReportsProgress = True
     BackgroundWorker1.RunWorkerAsync(New Object() {arguments})
    currentNumberOfFile += 1
    Next       
  End Sub
  
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    'uses the arguments
        Dim Bg_process As System.Diagnostics.Process = New System.Diagnostics.Process
              With Bg_process.StartInfo
                  .Arguments = str_arguments
                  .FileName = ffmpegPath
                  .CreateNoWindow = True
                  .UseShellExecute = False
                  .RedirectStandardOutput = True
                  .RedirectStandardError = True
              End With
  
              Bg_process.Start()
              Dim outputReader As StreamReader = Bg_process.StandardError
              Dim output As String
  
              While Not Bg_process.HasExited
                  output = outputReader.ReadLine()
                  BackgroundWorker1.ReportProgress(0, output)
                  Threading.Thread.Sleep(500)
              End While
        End Sub
  
   Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
     ' process args
  
  updateProgressStatus(args(0), args(1) )
  End Sub
  
  Private Function BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)  Handles BackgroundWorker1.RunWorkerCompleted
              Messagebox.Show(e.error.ToString)
  End Try
  
    Sub updateProgressStatus(ByVal progressValue As Integer, ByVal progressStatus As String)
  progressBar.value = progressValue
  lblprogressStatus.Text = progressStatus
        End Sub
  </code></pre>
  
  <p>The problem in here is the fnStartEvent method once gets started, it calls BackgroundWorker1.runWorkerAsync process and that runs in a seperate thread and does not wait for the thread to complete and it moves to the next line of code i.e loops to the next item and it returns to same BackgroundWorker1.runWorkerAsync line and throws exception that it is already running.</p>
  
  <p>Tried
  1. </p>
  
  <pre><code>    Private Sub fnStartEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.click
      Dim inputFolder = Directory.GetFiles(textboxSourceFolder.text) 
      Dim currentNumberOfFile As Integer=1
      For each Files in inputFolder
  
         Dim arguments As Object = Files
         updateProgressStatus(0, currentNumberOfFile)
         BackgroundWorker1.WorkerReportsProgress = True
         BackgroundWorker1.RunWorkerAsync(New Object() {arguments})
      ''
         Do Until BackgroundWorker1.IsBusy
         Thread.Sleep(500)
         Loop
  
    currentNumberOfFile += 1
    Next       
  End Sub
  </code></pre>
  
  <p>But this does not updates the Ui thread used to denote progress.</p>
  
  <p>2.Put the whole process inside the DoWork thread and loop for each file in there itself, but that to returns the cross thread error in progress bar updation.</p>
  
  <p>what is the standard procedure in executing a loop for the backgroundworker to wait for finishing as well as update the UI without blocking it. </p>