0

I have a problem with my code. I keep getting Multiple thread Error with backgroundworker, because of the combobox item display. Please look at my code below its a very simple code which I am planning to use on big scale, all I want it to do is "If item "1" selected show item "1" in label1. I can only assume that problem exists because Combobox runs in different thread....

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    BackgroundWorker1.runworkerasync()
    BackgroundWorker1.WorkerReportsProgress = True
    Me.Cursor = Cursors.WaitCursor 'Cursor changes to wait
End Sub
Public Structure controlwithtext
    Public controlname As Control
    Public text As String
    Public Sub New(ByVal ctrl As Control, ByVal text As String)
        Me.controlname = ctrl
        Me.text = text

    End Sub
End Structure

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork

If comboBox1.SelectedItem = "1" then
        BackgroundWorker1.ReportProgress(5, New controlwithtext(Label1, ComboBox1.SelectedItem))

End If

End Sub

Private Sub SetBackgroundWorker_ProgressChanged(ByVal sender As Object, 
            ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

    If TypeOf e.UserState Is controlwithtext Then
        Dim cwt As controlwithtext = CType(e.UserState, controlwithtext)
        cwt.controlname.Text = cwt.text
    End If

End Sub
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Alek
  • 35
  • 1
  • 9

1 Answers1

2

Here's an example of how to read from and write to controls from the BackgroundWorker thread:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    While True
        System.Threading.Thread.Sleep(250)

        Dim selection As String = Me.Invoke(Function()
                                                If Not IsNothing(ComboBox1.SelectedItem) Then
                                                    Return ComboBox1.SelectedItem.ToString
                                                Else
                                                    Return String.Empty
                                                End If
                                            End Function).ToString

        If selection = "1" Then
            Me.Invoke(Sub()
                          Label1.Text = ComboBox1.SelectedItem.ToString
                      End Sub)
        Else
            Me.Invoke(Sub()
                          Label1.Text = "something else"
                      End Sub)
        End If
    End While
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Thanks a lot !! Everything populates now ( I have multiple labels) Now for some reason it hangs after populating all the texts... Only if I disable "While" it works fine, but I understand while needs to be there for refresh? Or am I confusing something? Also "On Error GoTo" not supported with invoke? What would you recommend to use instead? Is "Try" a really good alternative? Thanks – Alek Oct 13 '15 at 04:13
  • The `While` was really just there to keep the worker thread alive and show that it was doing something. The **point** of the post was to show you how to read/write from the thread. Those two activities don't require a while loop. You need to figure out how to incorporate those examples into your existing code... – Idle_Mind Oct 13 '15 at 05:18