You
cannot can access the UI controls in the
DoWork
event handler by creating a delegate and use the Control.Invoke method(here Me.Invoke) which is explained in Wayne's answer. You can also access them in the
RunWorkerCompleted
event as shown below.
I will create a field of type List<int> to store values of a in the DoWork like this:
Private lst as List(Of Integer);
Private bgwTest as BackgroundWorker;
Public sub new()
{
lst = new List(Of Integer);
AddHandler(bgwTest.DoWork, AddressOf(bgwTest_DoWork))
AddHandler(bgwTest.RunWorkerCompleted, AddressOf(bgwTest_WorkCompleted))
}
Private Sub bgwTest_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwTest.DoWork
Dim a As Integer = 0
Do While a < 10
lst.Add(1);
Loop
End Sub
Private Sub bgwTest_WorkCompleted(sender as Object, e as RunWorkerCompletedEventArgs)
For Each i as Integer in lst
combobox1.Items.Add(i);
End For
End Sub
Another way to do is explained in Wayne's answer by using delegates.
Hope it helped. :)