向数据表添加行时发生StackOverflowException

时间:2012-11-30 13:27:55

标签: asp.net vb.net exception

使用递归来构建层次结构。

此递归在Windows Server 2003 + IIS6中正常工作但在Windows Server 2008 R2和IIS 7.0中抛出异常

以下是代码段:

Public Sub Expand(ByVal SE_Index As Int64)

    Dim row As DataRow

    If aryHierarchyData(SE_Index).Visable = True Then
        aryHierarchyData(SE_Index).Target_Row = gvCurrent_Row_Number
        gvCurrent_Row_Number = gvCurrent_Row_Number + 1

        Try
            row = dtExpand.NewRow
            row("SE_NO") = aryHierarchyData(SE_Index).SE_No
            row("Selection_Index") = aryHierarchyData(SE_Index).Selection_Index
            **dtExpand.Rows.Add(row)**--->>>throwing error "stackoverflow"

        Catch ex As StackOverflowException
            If (ex.Message.Contains("Column 'SE_NO, Selection_Index' is constrained to  be unique.")) Then
                Exit Sub
            End If

        Catch ex As Exception

        Finally
            row = Nothing
        End Try

        ' expand this SE's children  -  first
        If aryHierarchyData(SE_Index).Child > 0 Then
            Expand(aryHierarchyData(SE_Index).Child)
        End If
    End If

    ' expand this SE's Siblings  -  second

    If aryHierarchyData(SE_Index).Sibling > 0 Then
        Expand(aryHierarchyData(SE_Index).Sibling)
    End If

End Sub

2 个答案:

答案 0 :(得分:0)

此异常取决于计算机内存和框架。它与IIS和Windows服务器无关。

执行堆栈溢出时抛出的异常,因为它包含太多嵌套方法调用。此类无法继承。 MSDN

我尝试了一些更改代码。我根据自己的经验改变了上述陈述

框架4.0 执行堆栈溢出时抛出的异常,因为它包含 7573 嵌套方法调用。此类无法继承。

Framework 2.0 执行堆栈溢出时抛出的异常,因为它包含 7038 嵌套方法调用。此类无法继承。

答案 1 :(得分:0)

Stackoverflow problem could be solved using thread.
Call expand() under recursivethread function.

Private Sub RecurseThread(totalSeCount As Long)
 For iIdx = 1 To totalSeCount
    If aryHierarchyData(iIdx).Target_Row = 0 Then
    dtExpand.Clear()
    dtExpand.AcceptChanges()
    Expand(iIdx)
  End If
Next iIdx
End Sub

Dim t As Thread = New Thread(AddressOf RecurseThread, 1024 * 1024)
t.Start(totalSeCount)
t.Join()