-1

I'm getting an exception in this code:

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
Peladao
  • 4,036
  • 1
  • 23
  • 43
naveen sai
  • 11
  • 5
  • 3
    Side note: That empty `Catch` block is a very bad idea. Ignoring errors gets in the way of fixing errors. – David Dec 13 '11 at 14:45

3 Answers3

2

The problem is that you have a recursive call that never ends.

MDIParent1.MDIParent1() calls getApplications.getApplication(), which calls MDIParent1.MDIParent1(), which again calls getApplications.getApplication(), which calls MDIParent1.MDIParent1(), which again calls getApplications.getApplication(), which calls MDIParent1.MDIParent1(), which again calls getApplications.getApplication(), which calls MDIParent1.MDIParent1(), which again calls getApplications.getApplication(), which calls MDIParent1.MDIParent1(), which again calls getApplications.getApplication(), which calls MDIParent1.MDIParent1(), which again calls getApplications.getApplication(),

and so on into infinity (or until the system throws a StackOverflowException).

David
  • 72,686
  • 18
  • 132
  • 173
1

When you create an instance of MDIParent1 you call getApplication(). When you call getApplication() you create a new instance of MDIParent1. It's an infinite recursion resulting in a stack overflow exception.

What are you actually trying to do with this code?

David
  • 208,112
  • 36
  • 198
  • 279
  • +1 because you saw it, too, and answered in a less obnoxious way than I did. – David Dec 13 '11 at 14:57
  • @DavidStratton: I wouldn't call yours "obnoxious," though you could format it a little better :) – David Dec 13 '11 at 14:59
  • @David: I want to create a child form for each application processes..with mainwindowtitle as childform.text – naveen sai Dec 14 '11 at 14:37
  • @naveensai: Sounds like you need a second form other than `MDIParent1`. Then you'd create that second form in `getApplication()`, which would have been called once on `Form_Load()` of `MDIParent1`. This second, child form (a child of `MDIParent1`, I'd imagine, though I'm not familiar enough with WinForms development to be more specific) would _not_ call `getApplication()`. This would re-arrange your recursive loop into more of a tree structure (a shallow, wide tree, but a tree nonetheless). – David Dec 14 '11 at 15:06
0

Do you get the same Exception if you assign New separately from Dim?

Dim variable as DataType
// ...

Public Sub InitStuff()

    Set variable = New DataType
JOG
  • 5,590
  • 7
  • 34
  • 54