-1

我在这段代码中遇到了一个异常:

Imports System.Windows.Forms
Imports System.Text
Imports System.Diagnostics

Public Class MDIParent1
   Private Sub MDIParent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles  MyBase.Load
      getapp.getApplication()
   End Sub
   Public Sub MDIParent1(ByVal value As String, ByVal value1 As String)
      Dim ChildForm As New System.Windows.Forms.Form
      ChildForm.MdiParent = Me
      ChildForm.Text = value1
      ChildForm.Show()
   End Sub
End Class

Public Class getApplications

   Dim w As String
   Dim b As New Process()
   Dim p As String
   Dim mdi As New MDIParent1   'here i am getting exception that is System.StackOverflowException was unhandled   InnerException:..
   Dim i As Integer
   Public Sub getApplication()

      For Each Me.b In Process.GetProcesses(".")
         Try
            If b.MainWindowTitle.Length > 0 Then
               p = b.ProcessName.ToString()
               w = b.MainWindowTitle().ToString()
               mdi.MDIParent1(p, w)
            End If
         Catch
         End Try
      Next
   End Sub
End Class
4

3 回答 3

2

问题是你有一个永远不会结束的递归调用。

MDIParent1.MDIParent1() 调用 getApplications.getApplication(),后者调用 MDIParent1.MDIParent1(),后者再次调用 getApplications.getApplication(),后者调用 MDIParent1.MDIParent1(),后者再次调用 getApplications.getApplication(),后者调用 MDIParent1。 MDIParent1(),它再次调用getApplications.getApplication(),它调用MDIParent1.MDIParent1(),它再次调用getApplications.getApplication(),它调用MDIParent1.MDIParent1(),它再次调用getApplications.getApplication(),它调用MDIParent1 .MDIParent1(),它再次调用 getApplications.getApplication(),

以此类推直到无穷大(或直到系统抛出 StackOverflowException)。

于 2011-12-13T14:41:00.530 回答
1

当您创建一个实例时,MDIParent1您调用getApplication(). 当您调用时,getApplication()您会创建一个新的MDIParent1. 这是一个无限递归,导致堆栈溢出异常。

你实际上用这段代码做什么?

于 2011-12-13T14:43:15.197 回答
0

如果您将 New 与 Dim 分开分配,您会得到相同的异常吗?

Dim variable as DataType
// ...

Public Sub InitStuff()

    Set variable = New DataType
于 2011-12-13T14:41:37.510 回答