1

I wrote a program which is checking for mails. It is comparing the mails with a list in Excel. If the mail was found in the list as well, it is moving them.

I did include a Progress-bar Control in the program, and it is working. But not as it should be. Once the program moved all the mails, the Progress-bar Control starts to move.

After a lot of research I found out, that it's not possible to interact with the GUI. I need to 'stop' my Code first. I tried that, But none of the solutions I found did work out for me.

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
                ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
     'Do
     'Percentage = Count / (Anzahl_mail + 1)
     'Percentage = Percentage * 100
    Dim numToDo As Integer = CInt(e.Argument)
        For n As Integer = 1 To numToDo
            System.Threading.Thread.Sleep(100)
            BackgroundWorker1.ReportProgress(Convert.ToInt32((n / numToDo) * 100))
            'BackgroundWorker1.ReportProgress(Percentage)
        Next
        'Loop While Not BackgroundWorker1.CancellationPending
End Sub

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
                e As System.ComponentModel.ProgressChangedEventArgs) _
                        Handles BackgroundWorker1.ProgressChanged
    If ProgressBar1.Value = ProgressBar1.Maximum Then
        ProgressBar1.Value = ProgressBar1.Minimum
    End If
        Me.ProgressBar1.Value = DirectCast(e.ProgressPercentage, Integer)
        'Me.Invoke(New MethodInvoker(Sub() Me.ProgressBar1.Value = e.ProgressPercentage))
End Sub
evry1falls
  • 194
  • 4
  • 15
  • Does this answer your question? [Progress Bar and Background Worker](https://stackoverflow.com/questions/27565851/progress-bar-and-background-worker) – J. Scott Elblein Jun 11 '20 at 14:57
  • Your code works perfectly fine for me. I used that code and called `BackgroundWorker1.RunWorkerAsync(100)` and saw the `ProgressBar` gradually fill. Is that not what you're seeing? – jmcilhinney Jun 11 '20 at 15:04
  • 2
    I'm guessing you added that code specifically for a "progress bar", and left your mail code running in the UI thread (in a button click event handler probably). What you need to do is call `BackgroundWorker1.RunWorkerAsync()` from the button, then **put all the mail checking code in the actual `DoWork` handler**...raising progress along the way as appropriate. – Idle_Mind Jun 11 '20 at 16:18
  • Thank you for your fast respond. The progressbar is working now. – Gibby_Gibbson Jun 23 '20 at 12:12

0 Answers0