1

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

此递归在 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
4

2 回答 2

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()
于 2012-12-07T07:45:35.727 回答
0

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

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

我对您的代码进行了一些更改。我根据我的经验更改上述声明

Framework 4.0 执行堆栈溢出时抛出的异常,因为它包含7573 个嵌套方法调用。这个类不能被继承。

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

于 2012-11-30T15:02:34.443 回答