对于/ next循环挂起

时间:2016-03-15 19:31:18

标签: vb.net

我有批处理vb.net作业,有/ for循环对象和创建字母,在代码中向数据库插入行,偶尔完成作业并挂起一些时间,当使用Visiual Basic 2008运行时,它好吧从不挂起,当它在Windows服务器上运行时,偶尔会挂起,请你能帮我找到导致它挂起的原因。

 For i As Integer = 1 To myOverPmtContractInfos.Count
            'For Each myOverpmtContractInfo In myOverPmtContractInfos
            'Get all the contacts for the contract.
            If Not myDBOverpmtContact.retrieveAllByBGContract((myOverPmtContractInfos(i).billingGrp), (myOverPmtContractInfos(i).contractNbr), myOverpmtContacts) Then
                modErrorLog.WriteErrorLog(functionName & ": Error retrieving all contacts from Overpmt_Contacts for: " & myOverPmtContractInfos(i).billingGrp & " " & myOverPmtContractInfos(i).contractNbr)
                createLetters = False
                GoTo EndLetter
            End If
            'See if a second letter is due.
            If isLetterNeeded((myOverPmtContractInfos(i).daysAged), iInterval2) Then
                'Check to see if a second letter has already been processed.
                If Not doesCorrespondenceExist(myOverPmtContractInfos(i), iInterval2Type) Then
                    '  For j As Integer = 1 To myOverpmtContacts.Count
                    'For Each myOverpmtContact In myOverpmtContacts
                    'Populate a corrsepondence class object for the creation of the notice.
                    myOverpmtCorrespondence.billingGrp = myOverPmtContractInfos(i).billingGrp
                    myOverpmtCorrespondence.contractNbr = myOverPmtContractInfos(i).contractNbr
                    myOverpmtCorrespondence.corrNbr = myDBOverpmtCorrespondence.getNextNbr((myOverPmtContractInfos(i).billingGrp), (myOverPmtContractInfos(i).contractNbr))
                    myOverpmtCorrespondence.corrDate = todaysdate
                    myOverpmtCorrespondence.corrTypeId = iInterval2Type
                    myOverpmtCorrespondence.comments = ""
                    myOverpmtCorrespondence.postDate = todaysdate
                    myOverpmtCorrespondence.lastUpdated = todaysdate

                    If myOverpmtContacts.Count > 0 Then
                        modLetters.createCorrsepondence(myOverPmtContractInfos(i), myOverpmtContacts(1), myOverpmtCorrespondence)
                        iLetterCount = iLetterCount + 1
                        ' insertCorrespondence(myOverpmtCorrespondence)
                        modErrorLog.WriteErrorLog(functionName & ": 2nd Letter created for: " & myOverpmtCorrespondence.billingGrp & " / " & myOverpmtCorrespondence.contractNbr & " / " & myOverpmtContacts(1).firstname)
                    End If

                    If myOverpmtContacts.Count > 1 Then
                        modLetters.createCorrsepondence(myOverPmtContractInfos(i), myOverpmtContacts(2), myOverpmtCorrespondence)
                        iLetterCount = iLetterCount + 1
                        modErrorLog.WriteErrorLog(functionName & ": 2nd Letter created for: " & myOverpmtCorrespondence.billingGrp & " / " & myOverpmtCorrespondence.contractNbr & " / " & myOverpmtContacts(2).firstname)
                    End If

                    If myOverpmtContacts.Count > 2 Then
                        modLetters.createCorrsepondence(myOverPmtContractInfos(i), myOverpmtContacts(3), myOverpmtCorrespondence)
                        iLetterCount = iLetterCount + 1
                        modErrorLog.WriteErrorLog(functionName & ": 2nd Letter created for: " & myOverpmtCorrespondence.billingGrp & " / " & myOverpmtCorrespondence.contractNbr & " / " & myOverpmtContacts(3).firstname)
                    End If
                    GoTo EndLetter
                End If
            End If
            'See if a third letter is due.
            If isLetterNeeded((myOverPmtContractInfos(i).daysAged), iInterval3) Then
                'Check to see if a third letter has already been processed.
                If Not doesCorrespondenceExist(myOverPmtContractInfos(i), iInterval3Type) Then
                    'For Each myOverpmtContact In myOverpmtContacts
                    '   For k As Integer = 1 To myOverpmtContacts.Count
                    'Populate a corrsepondence class object for the creation of the notice.
                    myOverpmtCorrespondence.billingGrp = myOverPmtContractInfos(i).billingGrp
                    myOverpmtCorrespondence.contractNbr = myOverPmtContractInfos(i).contractNbr
                    myOverpmtCorrespondence.corrNbr = myDBOverpmtCorrespondence.getNextNbr((myOverPmtContractInfos(i).billingGrp), (myOverPmtContractInfos(i).contractNbr))
                    myOverpmtCorrespondence.corrDate = todaysdate
                    myOverpmtCorrespondence.corrTypeId = iInterval3Type
                    myOverpmtCorrespondence.comments = ""
                    myOverpmtCorrespondence.postDate = todaysdate
                    myOverpmtCorrespondence.lastUpdated = todaysdate

                    If myOverpmtContacts.Count > 0 Then
                        modLetters.createCorrsepondence(myOverPmtContractInfos(i), myOverpmtContacts(1), myOverpmtCorrespondence)
                        'modErrorLog.WriteErrorLog(functionName & ": Error creating 2nd Letter for: " & myOverpmtCorrespondence.billingGrp & " / " & myOverpmtCorrespondence.contractNbr)
                        iLetterCount = iLetterCount + 1
                        insertCorrespondence(myOverpmtCorrespondence)
                        modErrorLog.WriteErrorLog(functionName & ": Final Letter created for: " & myOverpmtCorrespondence.billingGrp & " / " & myOverpmtCorrespondence.contractNbr & " / " & myOverpmtContacts(1).firstname)
                    End If
                    If myOverpmtContacts.Count > 1 Then
                        modLetters.createCorrsepondence(myOverPmtContractInfos(i), myOverpmtContacts(2), myOverpmtCorrespondence)
                        'modErrorLog.WriteErrorLog(functionName & ": Error creating 2nd Letter for: " & myOverpmtCorrespondence.billingGrp & " / " & myOverpmtCorrespondence.contractNbr)
                        iLetterCount = iLetterCount + 1
                        modErrorLog.WriteErrorLog(functionName & ": Final Letter created for: " & myOverpmtCorrespondence.billingGrp & " / " & myOverpmtCorrespondence.contractNbr & " / " & myOverpmtContacts(2).firstname)
                    End If
                    If myOverpmtContacts.Count > 2 Then
                        modLetters.createCorrsepondence(myOverPmtContractInfos(i), myOverpmtContacts(3), myOverpmtCorrespondence)
                        'modErrorLog.WriteErrorLog(functionName & ": Error creating 2nd Letter for: " & myOverpmtCorrespondence.billingGrp & " / " & myOverpmtCorrespondence.contractNbr)
                        iLetterCount = iLetterCount + 1
                        modErrorLog.WriteErrorLog(functionName & ": Final Letter created for: " & myOverpmtCorrespondence.billingGrp & " / " & myOverpmtCorrespondence.contractNbr & " / " & myOverpmtContacts(3).firstname)
                    End If

                End If
            End If
EndLetter:
            'Reset the contacts collect for the next contract.
            'UPGRADE_NOTE: Object myOverpmtContacts may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
            myOverpmtContacts = Nothing
            myOverpmtContacts = New OverpmtContacts
        Next i

        createLetters = True

EndFunction:
        modErrorLog.WriteErrorLog("Number of letters created: " & iLetterCount)
        'UPGRADE_NOTE: Object myOverPmtContractInfos may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        myOverPmtContractInfos = Nothing
        'UPGRADE_NOTE: Object myDBOverpmtContractInfo may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        myDBOverpmtContractInfo = Nothing
        'UPGRADE_NOTE: Object myCodeTranslate may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        myCodeTranslate = Nothing
        'UPGRADE_NOTE: Object myOverpmtContacts may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        myOverpmtContacts = Nothing
        'UPGRADE_NOTE: Object myDBOverpmtContact may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
        myDBOverpmtContact = Nothing
        myOverpmtCorrespondence = New OverpmtCorrespondence
        myDBOverpmtCorrespondence = New DBOverpmtCorrespondence
    End Function 

1 个答案:

答案 0 :(得分:0)

如果某个应用程序有时会表现,但并非总是如此,那么它通常(但并不总是)由外部因素引起。

如果由外部因素引起:

  1. 考虑机器上正在运行的其他进程。它们会占用CPU吗?
  2. 考虑另一个应用程序是否将数据库绑定。如果您使用的是严重的DBMS(如Oracle或SQL Server),这是不太可能的,但值得研究。
  3. 考虑您的网络是否是问题。您是在尝试访问其他进程使用的资源(文件或文件夹),还是位于地理位置偏远的位置,只能通过相对较慢的连接访问?
  4. 如果您可以消除外部因素,请查看您的代码。

    1. 你是否在泄漏记忆?如果您没有及时正确地释放物体,可能会导致垃圾收集器因内存压力而启动。
    2. 您是否未足够快地关闭数据库连接并将其释放回连接池? (注意:请务必尽快释放数据阅读器。这些因为保持连接打开而臭名昭着。)
    3. 您是在谈论DCOM上的服务或组件吗?他们可靠吗?
    4. 您是否在调用Web服务?如果是这样,那么所有的投注都是关于他们的反应。你应该提供一种方法,允许他们在合理的时间范围内做出回应,然后优雅地失败。
    5. 您是否正在写入集中日志文件,数据库或队列?如果是这样,那么在执行此操作时如何管理并发?
    6. 这些只是我能想到的一些事情,当零星的行为出现时,应该考虑这些事情。