I am trying to loop through a listbox which contains filenames and upload them to an FTP server with a background worker. I am getting a cross-thread exception at my for loop when I attempt to access Listbox1.Items.Count within background worker (obviously because it's on a different thread) so I'm curious how I can pass the listbox into my background worker to execute the code they way I have written it below?
Private Sub bgw_upAllFiles_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw_upAllFiles.DoWork
Dim i
Dim toPath As String = MyForms.MoveOutFTPFormDir & PDFVar_PHOTO_URL_NUM & "/"
For i = 0 To e.Argument.Items.Count - 1
Try
retryDL:
My.Computer.Network.UploadFile(ListBox1.Items(i).ToString, toPath & IO.Path.GetFileName(ListBox1.Items(i).ToString), MyForms.MoveOutFTPUser, MyForms.MoveOutFTPPwd)
Catch ex As Exception
If ex.ToString.Contains("error: (550)") Then
'MsgBox("Need to create FTP folder")
Try
Dim myftprequest As Net.FtpWebRequest = CType(Net.FtpWebRequest.Create(toPath), System.Net.FtpWebRequest)
myftprequest.Credentials = New System.Net.NetworkCredential("JeffreyGinsburg", "andy86")
myftprequest.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory
myftprequest.GetResponse()
GoTo retryDL
Catch ex2 As Exception
ex2.ToString()
End Try
Else
MsgBox(ex.ToString)
End If
MDIParent1.StatusStrip.Items.Item(2).Text = "Upload Complete"
End Try
Next
End Sub