Hi all,
I'm calling a background worker to do task send file. But after the send file, it repeat back the same process. Why and how to solve this?
My code. I have simplify the code:
BUTTON SEND
Private Sub btn_send_Click(sender As System.Object, e As System.EventArgs) Handles btn_send.Click
Try
Dim checkedItems As ListView.CheckedListViewItemCollection = lvKiosk.CheckedItems
Dim item As ListViewItem
arr_sendkiosk = New ArrayList
For Each item In checkedItems
arr_sendkiosk.Add(item.SubItems(1).Text)
Next
If IsNothing(BackgroundWorker1) Then
BackgroundWorker1 = New BackgroundWorker
End If
If BackgroundWorker1.IsBusy <> True Then
' Start the asynchronous operation.
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End If
Catch ex As Exception
AppendNegative("Error send file.")
End Try
End Sub
BACKGROUNDWORKER_DOWORK
-After all required file is sending, it not go to RunComplete. It coming back to DoWork and sending back the same file.
Private Sub BackgroundWorker1_DoWork1(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
If (BackgroundWorker1.CancellationPending = True) Then
BackgroundWorker1.CancelAsync()
Exit Sub
End If
If cbx_images.Checked = True Then
For i = 0 To (arr_sendkiosk.Count - 1)
If StartSendingImages(i) Then
AppendPositive("Sending." & vbCrLf & vbCrLf)
Else
AppendNegative("Failed." & vbCrLf & vbCrLf)
End If
Next
End If
Catch ex As Exception
AppendNegative("Task not completed. Ex-" & ex.Message)
End Try
End Sub
BACKGROUNDWORKER_RUNCOMPLETED
Private Sub BackgroundWorker1_RunWorkerCompleted1(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled = True Then
AppendNormal("Task cancelled.")
ElseIf e.Error IsNot Nothing Then
AppendNegative("Error: " & e.Error.Message)
End If
lbl_progress.Text = "Background Process Complete.."
BackgroundWorker1.Dispose()
End Sub
*UPDATED FOR INVOKE FOR ANOTHER THREAD
Private Delegate Sub AddToMessageBoxDelegate(ByVal msg As String)
Public Sub AppendPositive(ByVal msg As String)
If Me.InvokeRequired Then
Dim d As New AddToMessageBoxDelegate(AddressOf Me.AppendPositive)
Me.BeginInvoke(d, New Object() {msg})
Else
Try
WritePositive(msg & vbCrLf)
Catch ex As Exception
End Try
End If
End Sub
Public Sub AppendNegative(ByVal msg As String)
If Me.InvokeRequired Then
Dim d As New AddToMessageBoxDelegate(AddressOf Me.AppendNegative)
Me.BeginInvoke(d, New Object() {msg})
Else
Try
WriteNegative(msg & vbCrLf)
Catch ex As Exception
End Try
End If
End Sub
Private Sub WritePositive(ByVal str As String)
Dim length As Integer = rtb_evt.TextLength
rtb_evt.AppendText(str)
rtb_evt.SelectionStart = length
rtb_evt.SelectionLength = str.Length
rtb_evt.SelectionColor = Color.Green
rtb_evt.Lines.Last()
End Sub
Private Sub WriteNegative(ByVal str As String)
Dim length As Integer = rtb_evt.TextLength
rtb_evt.AppendText(str)
rtb_evt.SelectionStart = length
rtb_evt.SelectionLength = str.Length
rtb_evt.SelectionColor = Color.Red
rtb_evt.Lines.Last()
End Sub