I'm setting up a simple app that has 2 variables, one from a combobox and one from a textbox.
Putting all my code directly in the button1_click sub works just fine, however, I wanted to add a marquee progress bar so I tried to implement a backgroundworker.
I am now aware that you can not call objects like combobox directly from a backgroundworker thread.
I've tried to declare a delegate with:
Create a sub:
and call it with
but it still throws the Cross-thread operation invalid error.
What the devil am I doing wrong?
Putting all my code directly in the button1_click sub works just fine, however, I wanted to add a marquee progress bar so I tried to implement a backgroundworker.
VB.NET:
Private WithEvents bgw As System.ComponentModel.BackgroundWorker
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProgressBar1.Show()
Button1.Text = "Please Wait"
bgw = BackgroundWorker1
bgw.WorkerSupportsCancellation = True
bgw.RunWorkerAsync()
End Sub
VB.NET:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim store As String = ComboBox1.Text & TextBox1.Text
Try
If My.Computer.Network.Ping(store & "p1") = True Then
'Map a drive
...
I am now aware that you can not call objects like combobox directly from a backgroundworker thread.
I've tried to declare a delegate with:
VB.NET:
Public Delegate Sub SetComboBoxTextDelegate(ByVal text As String)
Create a sub:
VB.NET:
Public Sub Store(ByVal text As String)
Dim store As String = ComboBox1.Text & TextBox1.Text
End Sub
and call it with
VB.NET:
Me.Invoke(New SetComboBoxTextDelegate(AddressOf SetComboBoxText), ComboBox1.Text)
but it still throws the Cross-thread operation invalid error.
What the devil am I doing wrong?