Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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

VB
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 ' at end of text
    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 ' at end of text
    rtb_evt.AppendText(str)
    rtb_evt.SelectionStart = length
    rtb_evt.SelectionLength = str.Length
    rtb_evt.SelectionColor = Color.Red
    rtb_evt.Lines.Last()
End Sub
Posted
Updated 30-Dec-14 21:10pm
v3
Comments
Shahan Ayyub 24-Dec-14 2:06am    
Can you please clarify on the following:
-Did you check the event do not get register twice ?
-Is 'StartSendingImages' is synchronous process ?
-Why not you empty the `arr_sendkiosk` array when you have already done the sending process so if it comes back, for loop will not work.
Luiey Ichigo 24-Dec-14 2:21am    
I have place the arr_sendkiosk = Nothing at RunWorkerCompleted before BackgroundWorker1.Dispose() but it still DoWork twice. I mean, after sending the image at DoWork, it not goes to RunWorkerCompleted and run it again. Even I put

If IsNothing(arr_sendkiosk) Then
Exit Sub
End If

before "If cbx_images.Checked = True Then", it not empty as it not goes to RunWorkerCompleted..

- StartSendingImages is Sub which done the transfering file to selected computer
Luiey Ichigo 24-Dec-14 2:35am    
And another things is, how to cancel the background process so anything that it done can be stop? Right now, I put the code on btn_cancel as below:-

If BackgroundWorker1.IsBusy = True Then
'If it supports cancellation, Cancel It
If BackgroundWorker1.WorkerSupportsCancellation Then
' Tell the Background Worker to stop working.
BackgroundWorker1.CancelAsync()
AppendNormal("Sending cancel by user...")
lbl_progress.Text = "Transfer task cancel by user."
End If
End If

the transfer process is still going through.
bojammis 24-Dec-14 11:55am    
What code is in StartSendingImages?

regs Ron O.
bojammis 24-Dec-14 12:14pm    
Ok - just duplicated your code, created stubs for members that you do not show code for. This code hits the runWorkerCompleted only once. This might indicate there is something going on in others members you call (AppendNegative, AppendPositive, StartSendingMessage() etc.). Would be nice to see what is going on with those areas.

regs

ron O.

1 solution

Found the solutions here..

http://stackoverflow.com/a/16899271[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900