Question

I have a BackGroundWorker with a For Each loop that is inside of a Try Catch and I need to detect the error and continue the For Each loop whit the next item.

Actually I have a list of data to send to a server trough UDP and wait for an ACK, but if the server didn't answer in 5 seconds the timeout error is cachet and the whole process is aborted.

I need to do something like this

Dim MyData_Array As String()
For Each MyData As String In MyData_Array

                MyData_Actual = MyData
                ' Translate the passed message into ASCII and store it as a Byte array.
                Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(MyData)
                Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)


                If data.Length = 67 Then
                    XMyData += 1
                    Dim Tx As New UdpClient()
                    Tx.Connect(Host, Port)
                    Tx.Client.SendTimeout = 5000
                    Tx.Client.ReceiveTimeout = 5000

                    Tx.Send(data, data.Length)
                    data = Tx.Receive(RemoteIpEndPoint)

                    Tx.Close()
                Else

                    MyData_ErrorList += MyData & vbCrLf

                End If
                'Report progress
                Porcentaje = (XMyData * 100) / MyData_Array.Count
                BackgroundWorker1.ReportProgress(Porcentaje, "Sending MyData " & XMyData.ToString & " de " & MyData_Array.Count.ToString & " : " & MyData)
                If BackgroundWorker1.CancellationPending Then
                    e.Cancel = True
                    Exit For
                End If
            Next

        End If

    Catch ex As TimeoutException
        MyData_ErrorList += MyData_Actual & vbCrLf
  '**********************************************************
        'Here need to delay 100mS and get back to the For Each to process the next item
  '**********************************************************
    Catch ex As Exception

        MyData_List = ex.Message & vbCrLf & "StackTrace: " & ex.StackTrace & vbCrLf & MyData_List
    End Try
Was it helpful?

Solution

Put the Try/Catch inside the for loop.

    Dim MyData_Array As String()
    For Each MyData As String In MyData_Array
      Try
        MyData_Actual = MyData
        ' Translate the passed message into ASCII and store it as a Byte array.
        Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(MyData)
        Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)


        If data.Length = 67 Then
            XMyData += 1
            Dim Tx As New UdpClient()
            Tx.Connect(Host, Port)
            Tx.Client.SendTimeout = 5000
            Tx.Client.ReceiveTimeout = 5000

            Tx.Send(data, data.Length)
            data = Tx.Receive(RemoteIpEndPoint)

            Tx.Close()
        Else

            MyData_ErrorList += MyData & vbCrLf

        End If
        'Report progress
        Porcentaje = (XMyData * 100) / MyData_Array.Count
        BackgroundWorker1.ReportProgress(Porcentaje, "Sending MyData " & XMyData.ToString & " de " & MyData_Array.Count.ToString & " : " & MyData)
        If BackgroundWorker1.CancellationPending Then
            e.Cancel = True
            Exit For
        End If
      Catch ex As TimeoutException
        MyData_ErrorList += MyData_Actual & vbCrLf

      Catch ex As Exception
        MyData_List = ex.Message & vbCrLf & "StackTrace: " & ex.StackTrace & vbCrLf & MyData_List
       'If you want to exit the For loop on generic exceptions, uncomment the following line
       'Exit For

      End Try
    Next             

OTHER TIPS

You might consider putting the try catch inside the for loop, if the iterative collection is not modified somehow by the error. Then handle the exception and continue the for loop.

How you have your loop and try/catch setup now will cause the entire loop to be broken out of on the timeout exception since the try/catch is placed outside the loop.

The your code would look more like this:

For Each MyData As String In MyData_Array
    Try
        'Perform code for each loop iteration
    Catch ex As TimeoutException
        'Handle Exception Here 
    End Try
Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top