0

I'm finding the background worker confusing. It will not allow me to write to a textbox in one of my functions. If I move the textbox.text into the BackgroundWork1_ProgressChanged it will allow it there, but doesn't do anything. If someone could explain it to me I'd greatly appreciate it.

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    txtImgCount.Text = ImageCount1
    fileCount.Text = fCount
    Label8.Text = statusText

    RichTextBox1.Text &= (fileFilename)
    RichTextBox1.Text &= (imgFilename)
    Dim i As Integer

    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = 200

    ProgressBar1.Value = e.ProgressPercentage
    lblStatus.Text = CType(e.UserState, String)
    lblStatus.Text = status & " " & e.ProgressPercentage.ToString & " % Complete "
End Sub
mightymax
  • 431
  • 1
  • 5
  • 16
  • Check out Control.Invoke: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invoke?view=netframework-4.8 – NoAlias Aug 26 '19 at 19:43
  • 2
    **NOPE**, don't `Invoke` in the `DoWork` method. You can pass an Object to `ProgressChanged` via the [ReportProgress(Int32, Object)](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker.reportprogress?) overload. See an example [here](https://stackoverflow.com/a/52015773/7444103). In the sample, class object is named `BGWorkerObject`. The class object is filled with the values that will be assigned to UI controls. – Jimi Aug 26 '19 at 19:47
  • @Jimi that made my head spin. I hate to say it but I don't understand. So if I have a label and I want it to update through the loop it's in right now I"m declaring a variable to hold the value then in the BGW_ProgressChanged event I update the label.text. What would be the better way to do this? – mightymax Aug 26 '19 at 19:55
  • Possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – JuanR Aug 26 '19 at 19:58
  • As already mentioned, `ReportProgress` lets you pass an object to `ProgressChanged`. That *object* can be a class (or anything else). Add to this class (or whatever) all the values (in properties, fields) needed to update your Controls. Don't ever reference UI controls in `DoWork`. – Jimi Aug 26 '19 at 20:02
  • I use BackgroundWorker1_ReportProgress(ImageCount1) and received the error too many arguments – mightymax Aug 26 '19 at 20:09
  • That's not a valid syntax for `ReportProgress`. You can either pass `(Int32)` or `(Int32, [an Object])`. There's no overload for just `([an Object])`. There's also an underscore in the code snippet, in your comment. See the Docs about `ReportProgress` functionality (the code sample I posted is quite clear about that, focus just on the `ReportProgress` stuff). – Jimi Aug 26 '19 at 20:25

0 Answers0