1,105,384 Community Members

Deallocate memory at runtime for Dymamic created Controls

Member Avatar
ashishgh
Newbie Poster
6 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have created an application which creates multiple webbrowser controls at runtime...now i want to clean up memory when these webrowsers are deleted see the code below please let me know how can i do it(i have tried dispose method but no luck)

Creation on dynamic web browsers on button click

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim i = 0
        Dim y = 0
        For i = 0 To (ListBox1.SelectedIndices.Count - 1)
            Dim t As System.Windows.Forms.AnchorStyles = AnchorStyles.Top
            Dim b As System.Windows.Forms.AnchorStyles = AnchorStyles.Bottom
            Dim r As System.Windows.Forms.AnchorStyles = AnchorStyles.Right
            Dim s = ListBox1.Items(ListBox1.SelectedIndices.Item(i)).ToString()
            Dim TT As New TEST_TABS
            TT.wb = New WebBrowser
            TT.wb.AllowNavigation = True
            TT.wb.Navigate(s)
            TT.wb.Location = New Point(0, 0)
            TT.wb.Size = TabControl1.Size
            TT.wb.Name = s+"e"
            TT.wb.ScriptErrorsSuppressed = True

            '//////////////////////////////////////////////////////////////////////////'

            ' TT.wb.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            '   Or System.Windows.Forms.AnchorStyles.Left) _
            ' Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            '///////////////////////////////////////////////////////////////////////////'
            TT.pb = New ProgressBar
            TT.pb.Name = s + "p"
            TT.pb.Location = New Point(y, 0)
            TT.pb.Size = New Size(125, 24)
            'TT.pb.Size = TabControl1.Size
            TT.pb.Visible = True
            Me.SplitContainer1.Panel2.Controls.Add(TT.pb)
            y += 126
            Dim tab_page As New TabPage
            tab_page.Name = s
            tab_page.Text = s
            tab_page.Font = New System.Drawing.Font("Calibri", 22)
            tab_page.Controls.Add(TT.wb)

            TabControl1.Controls.Add(tab_page)
            TabControl1.SelectedIndex = i

        Next i
        i = 0

        For Each tp As TabPage In TabControl1.TabPages

            ListBox1.Items.Remove(tp.Name)
        Next

        Button1.Enabled = False

        Button2.Enabled = True
        Panel1.Hide()
    End Sub

I want to remove memory used by these dynamic webbrowsers on below click

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        For Each tp As TabPage In TabControl1.TabPages

            tp.Controls.Item(0).Dispose()
            Me.SplitContainer1.Panel2.Controls.Item(tp.Name.ToString + "p").Dispose()
        Next

        'TabControl1.TabPages.Clear()
        Button1.Enabled = True

        Panel1.Show()     
     

    End Sub

Below is the TEST_TABS class

Public Class TEST_TABS
    Public WithEvents wb As New WebBrowser
    Public WithEvents pb As New ProgressBar

    Dim page_counter As Integer = 0


    Private Sub wb_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles wb.DocumentCompleted
        ' MsgBox(page_counter)
        page_counter += 1

        If wb.DocumentText.Contains(WindowsApplication1.My.Resources.Resource1.loginpage) Then

            wb.Document.GetElementById("username-id").SetAttribute("value", Form1.ComboBox1.SelectedItem.ToString)
            wb.Document.GetElementById("pwd-id").SetAttribute("value", Form1.ComboBox1.SelectedItem.ToString)
            wb.Document.GetElementById("login").InvokeMember("click")

        End If
        If page_counter = 1 Then

        End If
    End Sub

    Private Sub wb_ProgressChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles wb.ProgressChanged
        wb.Size = Form1.TabControl1.Size


        If (e.CurrentProgress >= 0 And e.CurrentProgress < 100) Then

            pb.Maximum = 100
            pb.Value = Convert.ToInt32(e.CurrentProgress)

        End If
    End Sub
End Class
Member Avatar
Reverend Jim
Noli mentula
5,394 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

I came across this piece of code once while trying to solve a similar problem. See if this helps.

Private Sub releaseObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try

End Sub

According to the person who posted it, it is supposed to release the passed object then force garbage collection to run to reclaim the space.

Member Avatar
ashishgh
Newbie Poster
6 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Jim,

Thanks for your releaseobject() method...although i was not able to pass the dynamic objects created (TT.wb and TT.pb)under Button1.click method into releaseObject()

I have called GC.collect() just as i dispose these dynamic controls and Yippi :icon_cheesygrin: my memory was regained on runtime

For Each tp As TabPage In TabControl1.TabPages
            tp.Controls(0).Dispose()
            Me.SplitContainer1.Panel2.Controls(tp.Name.ToString + "p").Dispose()
            tp.Dispose()
            GC.Collect()
        Next

I have a query... Q: How can we access the objects instance created at runtime in a particular method (in my case under Button1.click) FROM any other menthod or outside of the creator method ???

Member Avatar
Reverend Jim
Noli mentula
5,394 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 
Public Class Form1
  
    Public mytextbox as New TextBox
    .
    .
    .
End Class

Public Class Form2
    .
    .
    .
    MsgBox(Form1.mytextbox.Text)
    .
    .
    .
End Class
Member Avatar
ashishgh
Newbie Poster
6 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Well thats accessing objects accross two classes and thats fine...

I want to access some things like below

Public class something

Sub one()

Dim TB as New textBox

End Sub



Sub two()

'How to access TB here ?? or from anywhere in app domain

End Sub



End class
Member Avatar
Reverend Jim
Noli mentula
5,394 posts since Aug 2010
Reputation Points: 746 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

Same thing. Declare it at the class level and it will have class scope. If you are only using it inside the class then you can make it private.

Member Avatar
codeorder
Postaholic
2,027 posts since Aug 2010
Reputation Points: 197 [?]
Q&As Helped to Solve: 390 [?]
Skill Endorsements: 10 [?]
 
0
 

Add a "cool" Parameter to your Sub.

Sub two(byval selCoolTextBox as TextBox)

   'How to access TB here ?? or from anywhere in app domain
   msgbox(selCoolTextBox.text)
End Sub

And call it as:

private sub someBtn.Click
   two(TB)
End Sub
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: