0

我有一个带有 For Each 循环的 BackGroundWorker,它位于 Try Catch 内,我需要检测错误并继续 For Each 循环并处理下一项。

实际上,我有一个要通过 UDP 发送到服务器并等待 ACK 的数据列表,但是如果服务器在 5 秒内没有响应,则会缓存超时错误并且整个过程中止。

我需要做这样的事情

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
4

2 回答 2

2

将 Try/Catch 放在 for 循环中。

    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             
于 2013-07-12T15:59:29.363 回答
1

如果迭代集合没有被错误以某种方式修改,您可能会考虑将 try catch 放在 for 循环中。然后处理异常并继续for循环。

您现在如何设置循环和 try/catch 将导致整个循环因超时异常而中断,因为 try/catch 位于循环之外。

您的代码看起来更像这样:

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
于 2013-07-12T16:02:07.703 回答