AdorableVB
AdorableVB

Reputation: 1393

how to dispose all hidden forms?

I am having a problem on memory loads of my app..
Since it uses Dim f As New Form2 every time I want to view something, it adds up to the load. Though I cannot close it since I will not be able to use the form again, instead I hide it.
but I am not calling the f that I have hidden, instead I create another f..

from time to time.. if I open, and close, and open again, those f's will be on the memory of a 24/7 application and that's a really heavy load I might say.

I have read about garbage collection and did some tests, and I think that helps, I guess.

here's the math of my app in the task manager :

Mainform -- 30mb --> then
I Dim f As New Form2 and Hide() 20 f's -- it became --> 57mb
after 5mins -- 50mb
this is where I think that GC occured.. or am I wrong.

that's about it explaining the current problem..
So, do I have an option like..

dim forms = application.hidden.ofType(form2)
'close em'

OR

'teach me some workaround, that would be great :)

OR No Choice but

'do nothing.. :/

Upvotes: 0

Views: 199

Answers (1)

EDIT

If you dont want to restructure the code into a proper class, then the thing that creates form instances must be able to find the form/cams it creates so it can close them. To do that it needs a) an identifier, b) a storage mechanism and c) a way to know the user is closing a form - you are missing at least 2 of those things.

a) an identifier

when mainform creates a form instance add something to the constructor like a name or tag. This must be something the user cannot change. This uses a GUID:

MainForm, probably a click event:

  Dim CamID As String = System.Guid.NewGuid.ToString
  Dim f as New Form2(CamID)
  ' more to come in a moment

Form2:

 Public CamGuid As String = ""

 ' modify **existing** Sub New adding a param
 Public Sub New(myCamID As String) 
   ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    CamGuid = myCamID
 End Sub

Now both the mainform and the camform have a common, unique identifier!

b) Storage

The Mainform needs to be able to find that f to close it properly:

  Private _cameras As New Dictionary(of String, Form2)

In that same click event, save the form ref just created:

  Dim CamID As String = System.Guid.NewGuid.ToString
  Dim f as New Form2(CamID)
  ' save form ref using CamID as a key
  _cameras.Add(CamID, f)

c) Closing notice: In form2 clos*ING* event:

   Me.Hide
   ' tell main form we are closing, identify which one using the camera GUID
   MainForm.CameraClosed(CamGuid)

Note: Put normal close code (like clean up and killing the camera feed) in CLOSE - we need a way for that to run normally. So, put the HIDE and Call to MainForm in FormClosing.

In Main Form:

   Public Sub CameraClosed(cameraID As String)

        if _cameras.ContainsKey(cameraID) Then
            ' doing it the long way...fetch the form ref
            Dim f As Form2 = _cameras(CameraID)
            ' remove from dictionary of active forms
            _cameras.Remove(cameraID)
            f.Close            ' let normal CLOSE code run
            f = Nothing
        End if
   End Sub

Upvotes: 1

Related Questions