做了一个 递归查找树节点 的程序,
功能 是 从当前节点 查找,
查找条件 是 节点文本 = 要求文本 ,
如果没有找到,最后提示消息:没有找到。程序运行1年都没有错误,今天却在一棵树上进行查找时,报下面的错误:
未处理的“System.StackOverflowException”类型的异常出现在 mscorlib.dll 中。
NET 的排错提示是 : 确保你没有无限循环或没有无限递归。于是我用次程序在另外的树上查找,程序能够正常运行,即便是找不到要求的节点,也能按照设计的报错。不同的是两颗树,出错的树节点好像要多一些。分析应该没有出现无限循环或无限递归,因为如果那样的话,在节点少的树上,如果找不到的话,也应该出错。请问大虾们,net 的递归是不是有层数限制,如果是,那么是几层?或者请大虾们讲讲这是什么原因?
功能 是 从当前节点 查找,
查找条件 是 节点文本 = 要求文本 ,
如果没有找到,最后提示消息:没有找到。程序运行1年都没有错误,今天却在一棵树上进行查找时,报下面的错误:
未处理的“System.StackOverflowException”类型的异常出现在 mscorlib.dll 中。
NET 的排错提示是 : 确保你没有无限循环或没有无限递归。于是我用次程序在另外的树上查找,程序能够正常运行,即便是找不到要求的节点,也能按照设计的报错。不同的是两颗树,出错的树节点好像要多一些。分析应该没有出现无限循环或无限递归,因为如果那样的话,在节点少的树上,如果找不到的话,也应该出错。请问大虾们,net 的递归是不是有层数限制,如果是,那么是几层?或者请大虾们讲讲这是什么原因?
解决方案 »
- 调用WSDL生成的webservice代理类报错 [(407) Proxy Authentication Required]
- 关于异步处理消息的业务-----===========-------在线等等等
- 求C#socket通信代码
- 求助一些资料,因为实在找不到自己想要的。
- datgridview控件combobox联动出现DataGridViewComboBoxCell值无效的问题。
- 读取位置冲突
- winform下的DataGrid查询时间的问题
- 调试工具(DbgView)这个 工具有哪位达人用过,怎么用
- 高分求制作UML图形的控件,等着救命,ActiveX控件也行。
- 一个对arraylist的赋值问题!
- using块的应用
- 《C#入门经典》这本书到底怎么样?。。
操作系统的书上都有说到的~~我遇到过好多次"确保你没有无限循环或没有无限递归"开始的时候都认为没有错误,可到最后还是IDE对了~~2楼说的没错,仔细检查下这棵出错的树有什么特别
好的。大家看看代码
''' <summary>
''' 查找树节点:下一个,直接在树上寻找
''' </summary>
''' <param name="txt"></param>
''' <res></res>
Public Function M_FindTreeNode_Next(ByRef tree As TreeView, ByVal txt As String, Optional ByVal isExact As Boolean = False) As TreeNode
Dim pt1 As New PL_Base.PL_Tools
Dim node0 As TreeNode
Dim node1 As TreeNode
If tree.SelectedNode Is Nothing Then
node0 = tree.Nodes(0)
Else
node0 = tree.SelectedNode
End If If node0 Is Nothing Then
Throw New Exception("没有找到指定内容的节点!")
Else
node1 = pt1.M_FindTreeNode_SpecifyText(node0, txt, isExact)
If Not node1 Is Nothing Then
tree.SelectedNode = node1
Return node1
Else
Throw New Exception("没有找到指定内容的节点!")
End If
End If
End Function''' 查找树节点:指定文本
''' </summary>
''' <param name="xa_node"></param>
''' <param name="txt1"></param>
''' <returns></returns>
''' <res></res>
Public Function M_FindTreeNode_SpecifyText(ByVal xa_node As TreeNode, ByVal txt1 As String, Optional ByVal isExact As Boolean = False) As TreeNode
Dim result As TreeNode
Dim v_node As TreeNode
v_node = M_FindTreeNode_Next(xa_node)
Dim isFind As Boolean '找到
sum = sum + 1 '出问题时加的,到6988次,开始出堆栈超出的错误
If sum = 6998 Then
sum = sum
End If If Not v_node Is Nothing Then
If isExact = False Then
isFind = v_node.Text.Contains(txt1)
Else
isFind = (v_node.Text = txt1)
End If If isFind = True Then
result = v_node
Else
result = M_FindTreeNode_SpecifyText(v_node, txt1, isExact)
End If
End If
Return result End Function Public sum As Integer ''' <summary>
''' 查找树节点:某节点的下一个
''' </summary>
''' <param name="xa_node"></param>
''' <returns></returns>
''' <res></res>
Public Function M_FindTreeNode_Next(ByVal xa_node As TreeNode) As TreeNode
Dim parent_bur As TreeNode If Not xa_node.FirstNode Is Nothing Then
Return xa_node.FirstNode
End If If Not xa_node.NextNode Is Nothing Then
Return xa_node.NextNode
End If parent_bur = M_FindTreeNode_MaxNuncle(xa_node)
Return parent_bur
End Function
''' <summary>
''' 查找树节点:最大的叔或叔祖
''' </summary>
''' <param name="xa_node"></param>
''' <returns></returns>
''' <res></res>
Public Function M_FindTreeNode_MaxNuncle(ByVal xa_node As TreeNode) As TreeNode
Dim parent_bur As TreeNode
If xa_node.Parent Is Nothing Then
Return parent_bur
End If
If xa_node.Parent.NextNode Is Nothing Then
parent_bur = M_FindTreeNode_MaxNuncle(xa_node.Parent)
Else
parent_bur = xa_node.Parent.NextNode
End If
Return parent_bur
End Function
''' <summary>
''' 查找树节点:下一个,直接在树上寻找
''' </summary>
''' <param name="txt"> </param>
''' <res> </res>
Public Function M_FindTreeNode_Next(ByRef tree As TreeView, ByVal txt As String, Optional ByVal isExact As Boolean = False) As TreeNode
Dim pt1 As New PL_Base.PL_Tools
Dim node0 As TreeNode
Dim node1 As TreeNode
If tree.SelectedNode Is Nothing Then
node0 = tree.Nodes(0)
Else
node0 = tree.SelectedNode
End If If node0 Is Nothing Then
Throw New Exception("没有找到指定内容的节点!")
Else
node1 = pt1.M_FindTreeNode_SpecifyText(node0, txt, isExact)
If Not node1 Is Nothing Then
tree.SelectedNode = node1
Return node1
Else
Throw New Exception("没有找到指定内容的节点!")
End If
End If
End Function ''' 查找树节点:指定文本
''' </summary>
''' <param name="xa_node"> </param>
''' <param name="txt1"> </param>
''' <returns> </returns>
''' <res> </res>
Public Function M_FindTreeNode_SpecifyText(ByVal xa_node As TreeNode, ByVal txt1 As String, Optional ByVal isExact As Boolean = False) As TreeNode
Dim result As TreeNode
Dim v_node As TreeNode
v_node = M_FindTreeNode_Next(xa_node)
Dim isFind As Boolean '找到
sum = sum + 1 '出问题时加的,到6988次,开始出堆栈超出的错误
If sum = 6998 Then
sum = sum
End If If Not v_node Is Nothing Then
If isExact = False Then
isFind = v_node.Text.Contains(txt1)
Else
isFind = (v_node.Text = txt1)
End If If isFind = True Then
result = v_node
Else
result = M_FindTreeNode_SpecifyText(v_node, txt1, isExact)
End If
End If
Return result End Function Public sum As Integer ''' <summary>
''' 查找树节点:某节点的下一个
''' </summary>
''' <param name="xa_node"> </param>
''' <returns> </returns>
''' <res> </res>
Public Function M_FindTreeNode_Next(ByVal xa_node As TreeNode) As TreeNode
Dim parent_bur As TreeNode If Not xa_node.FirstNode Is Nothing Then
Return xa_node.FirstNode
End If If Not xa_node.NextNode Is Nothing Then
Return xa_node.NextNode
End If parent_bur = M_FindTreeNode_MaxNuncle(xa_node)
Return parent_bur
End Function
''' <summary>
''' 查找树节点:最大的叔或叔祖
''' </summary>
''' <param name="xa_node"> </param>
''' <returns> </returns>
''' <res> </res>
Public Function M_FindTreeNode_MaxNuncle(ByVal xa_node As TreeNode) As TreeNode
Dim parent_bur As TreeNode
If xa_node.Parent Is Nothing Then
Return parent_bur
End If
If xa_node.Parent.NextNode Is Nothing Then
parent_bur = M_FindTreeNode_MaxNuncle(xa_node.Parent)
Else
parent_bur = xa_node.Parent.NextNode
End If
Return parent_bur
End Function[/Quote]
Try
M_FindTreeNode_Next(Me.TreeView1, "待查找文本", Me.P_IsExact)
Catch e1 As Exception
End Try
End Sub
那面感觉人气少,不论vb还是 c# 道理是差不多啦,大家分析下啊。
我觉得递归的时候都有出口了。程序主要是两处出现递归
第1处
M_FindTreeNode_SpecifyText 调用 M_FindTreeNode_SpecifyText,结束条件 v_node Is Nothing
第2处
M_FindTreeNode_MaxNuncle 调用 M_FindTreeNode_MaxNuncle 结束条件 not xa_node.Parent.NextNode Is Nothing大家分析下,希望能找到原因。先吃饭了。
If sum = 6998 Then
sum = sum
End If 这一段代码是出问题后加的,当sum = 6998 执行
isFind = v_node.Text.Contains(txt1)
就出错:未处理的“System.StackOverflowException”类型的异常出现在 mscorlib.dll 中。
NET 的排错提示是 : 确保你没有无限循环或没有无限递归。