title: Progress bar is not working in BackgroundWorker VB.Net
body: <p>I'm trying to get a progress bar to show while a method is being executed. I've called BackgroundWorker1.RunWorkerAsync() from a button. Then in the DoWork I call the method to run "runCopyFiles". I update the progress in ProgressChanged, and exit with RunWorkerCompleted. I'm not sure what I have wrong. I've searched the net for tutorials and examples on how to do this and have created the code from them. But the progress bar doesn't show.</p>
  
  <p>Code For executing from button</p>
  
  <pre><code>Private Sub btnExecuteFileCopy_Click(sender As Object, e As EventArgs) Handles btnExecuteFileCopy.Click
      Me.Refresh()
      If Not BackgroundWorker1.IsBusy = True Then
          BackgroundWorker1.RunWorkerAsync()
      End If
  End Sub
  </code></pre>
  
  <p>Code for BackGroundWorker</p>
  
  <pre><code>Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
      BackgroundWorker1.WorkerReportsProgress = True
      Const Max As Integer = 1000
  
      For i = 1 To Max
          '' do something
          '' (I put a sleep to simulate time consumed)
          Threading.Thread.Sleep(100)
  
          '' report progress at regular intervals
          BackgroundWorker1.ReportProgress(CInt(100 * i / Max), "Running..." &amp; i.ToString)
  
          '' check at regular intervals for CancellationPending
          If BackgroundWorker1.CancellationPending Then
              BackgroundWorker1.ReportProgress(CInt(100 * i / Max), "Cancelling...")
              Exit For
          End If
      Next
      runCopyFiles()
  End Sub
  
  Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
      ProgressBar1.Value = e.ProgressPercentage
      lblStatus.Text = e.ProgressPercentage.ToString() + " %"
  End Sub
  
  Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
      If e.Cancelled = True Then
          MsgBox(" Operation Cancelled ")
          ProgressBar1.Value = 0
          lblStatus.Text = ""
      ElseIf e.Error IsNot Nothing Then
          MsgBox(e.Error.Message)
      Else
          MsgBox(" Process Complete ")
      End If
  End Sub
  </code></pre>
  
  <p>Code RunCopy</p>
  
  <pre><code>Private Sub runCopyFiles()
  
      Application.UseWaitCursor = True
      Application.DoEvents()
      Me.Refresh()
  
      Dim sFileToFind As String
      Dim location As String
      Dim File As String
      'Dim createReportFldr As String
      'Dim createXMLFldr As String
      'Dim createImgFldr As String
  
      'Directory Files are located in
      location = txtFolderPath.Text
      'Directory files are to copied into
      MoveLocation = CopyToPath
  
      createImgFldr = MoveLocation &amp; "\Figures"
      createReportFldr = MoveLocation &amp; "\Reports"
      createXMLFldr = MoveLocation &amp; "\XML files"
  
      'Create Figures Folder
      If Not IO.Directory.Exists(createImgFldr) Then
          IO.Directory.CreateDirectory(createImgFldr)
          ' MsgBox("folder created" &amp; createFolder)
      End If
      'Create Reports folder
      If Not IO.Directory.Exists(createReportFldr) Then
          IO.Directory.CreateDirectory(createReportFldr)
          'MsgBox("folder created" &amp; createReportFldr)
      End If
      'Create XML folder
      If Not IO.Directory.Exists(createXMLFldr) Then
          IO.Directory.CreateDirectory(createXMLFldr)
          ' MsgBox("folder created" &amp; createFolder)
      End If
  
  
      orphanedFiles = MoveLocation &amp; "\Reports\OrphanedFilesItems.txt"
      ' Create or overwrite the file.
      System.IO.File.Create(orphanedFiles).Dispose()
  
      ListofFiles = MoveLocation &amp; "\Reports\ListOfFiles.txt"
      ' Create or overwrite the file.  
      System.IO.File.Create(ListofFiles).Dispose()
  
      MissingFiles = MoveLocation &amp; "\Reports\MissingGraphicList.txt"
      ' Create or overwrite the file.  
      System.IO.File.Create(MissingFiles).Dispose()
  
      Dim FILE_NAME As String
  
      FILE_NAME = txtFileName.Text
  
      Dim objReader As New System.IO.StreamReader(FILE_NAME)
      Dim sFile As String
      Do While objReader.Peek() &lt;&gt; -1
          File = objReader.ReadLine()
          sFileToFind = location &amp; "\" &amp; File &amp; "*.*"
          sFile = File
  
          Dim paths() As String = IO.Directory.GetFiles(location, sFile, IO.SearchOption.AllDirectories)
          If paths.Count = 0 Then
              System.IO.File.AppendAllText(orphanedFiles, sFile &amp; vbNewLine)
              ' Debug.Print(File)
              'If System.IO.File.Exists(orphanedFiles) = True Then
              '    Dim objWriter As New System.IO.StreamWriter(orphanedFiles, IO.FileMode.Append)
              '    objWriter.WriteLine(File)
              '    objWriter.Close()
              'Else
              'MsgBox("Creating Orphaned File Now. ")
              'End If
          Else
              For Each pathAndFileName As String In paths
                  Dim moveToFolder As String
  
                  If System.IO.File.Exists(pathAndFileName) = True Then
                      Dim sRegLast As String = pathAndFileName.Substring(pathAndFileName.LastIndexOf("\") + 1)
                      Dim toFileLoc As String
  
                      'MsgBox("sRegLast " &amp; sRegLast)
                      ' fileLoc = MoveLocation &amp; sRegLast
                      moveToFolder = MoveLocation &amp; "\XML files\" &amp; sRegLast
                      toFileLoc = createXMLFldr &amp; "\" &amp; sRegLast
                      'MsgBox("FileLoc " &amp; fileLoc)
  
                      'if toFileLoc = XML file exists move it into the XML files folder
                      If System.IO.File.Exists(toFileLoc) = False Then
                          System.IO.File.Copy(pathAndFileName, moveToFolder)
                          System.IO.File.AppendAllText(ListofFiles, sRegLast &amp; vbNewLine)
                      End If
                  End If
              Next
          End If
      Loop
      'MsgBox("Files have been moved")
  
      Call CreateGraphicsFunction(Nothing, System.EventArgs.Empty)
      Call getImages()
      MsgBox("Process Complete", MsgBoxStyle.DefaultButton1)
      Application.UseWaitCursor = False
      Application.DoEvents()
      ' Me.Close()
  End Sub
  </code></pre>