Results 1 to 14 of 14

Thread: [RESOLVED] Howto Dispose of Child Form?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Chesterfield, UK
    Posts
    162

    Resolved [RESOLVED] Howto Dispose of Child Form?

    I have a MDI application with each menu option taking the following code

    VB Code:
    1. private sub menu1_click ...
    2.   dim x as new myclass.program
    3.   x.mdiparent=me
    4.   x.show()
    5. end sub

    I also test for open child forms by looping through me.children when I try to shutdown the app.
    VB Code:
    1. Dim openforms As Boolean = False
    2. For Each x In Me.MdiChildren
    3.   openforms = True
    4. Next

    The problem I would like to solve is, when the child form is loaded and closed the 'dll' is still running in memory. Is there anyway I can dispose of 'closed' forms? or dispose of anything that is no longer in use?

    Thanks

  2. #2
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Howto Dispose of Child Form?

    try
    For Each x As System.Windows.Forms.Control In Me.MdiChildren
    x.Dispose()
    Next

    calling the dispose method tells
    vb.net to go ahead and handle the
    garbage collection.
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Chesterfield, UK
    Posts
    162

    Re: Howto Dispose of Child Form?

    Thanks, I tried that but its made no difference. The only reason I want to dispose of the 'myclass.dll' is so I can copy a new version in without the need to come all the way out of the MDI app.

    If I load the MDI app it allows me to copy in 'myclass.dll', but as soon as I run the menu option and then close the child form down I get a sharing violation.

    I've tried to dispose of the child form when it closes but this made no difference.

  4. #4
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Howto Dispose of Child Form?

    how are you loading the dll, if I may ask ?
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Chesterfield, UK
    Posts
    162

    Re: Howto Dispose of Child Form?

    If you look at my first post it show's a sample menu option.

    Basically 'x' is declared as the class within each menu option and then 'x.show' displays then form. I think the problem is that I can't dispose of 'x' because I cant reference it from outside of the menu option. I do however need a menu option to be shown more than once.

  6. #6
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Howto Dispose of Child Form?

    If you are just referincing x from the menu click event:

    private sub menu1_click ...
    dim x as new myclass.program
    x.mdiparent=me
    x.show()
    end sub

    you will have problems because x is limited to within the menu1_click event.

    Instead try something like this:

    below the windows generated code:

    private x as new myclass.program

    This will make it availabe to the whole class instead of inside
    the menu click event.
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Chesterfield, UK
    Posts
    162

    Re: Howto Dispose of Child Form?

    I thought as much, the downside is I will need to create a private variable for each menu option and maybe some kind of array so I can initiate multiple instances.

    I was just hoping of maybe a global 'dispose' command which will remove anything thats no longer running.

    Thanks anyway.

  8. #8
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Howto Dispose of Child Form?

    laugh, sorry for the bad news....
    Ive had to do something similar,

    Maybe you can create some strFormToLoad and
    depending on that value, get away with something
    like frmMDI_A.hide, frmMDI_B.ShowDialog

    ...just shooting of the top of my head...

    let me know if I can be of any more help...
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  9. #9
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: Howto Dispose of Child Form?

    Hi

    This is interesting because I soon have to implement something similar. Can I suggest adding GC.Collect() your code

    VB Code:
    1. try
    2. For Each x As System.Windows.Forms.Control In Me.MdiChildren
    3. x.Dispose()
    4. Next
    5. GC.Collect()   ' This will effectively release the object from memory.

    I'm just guessing here because I'm assuming the Dispose method just tells the .NET runtime that the object can be released the next time the garbage collector runs.

    Alternatively you could raise an event to the mdi container from your DLL when it unloads.
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Chesterfield, UK
    Posts
    162

    Re: [RESOLVED] Howto Dispose of Child Form?

    Adding GC.COLLECT made no difference.

  11. #11
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: [RESOLVED] Howto Dispose of Child Form?

    Adding GC.COLLECT made no difference.
    Oops

    Ok I think I know why the things are still in memory. As a bgard68 stated, because you declaring a variable in a method and then instantiating the DLL there's no way for you to reference that variable outside the method and then to set it to nothing. Using an array of private variables doesn't seem a good idea but I think thats the route you need to take. Here's how I plan to implement my solution.

    • Define an interface that will have a ReleaseMe event plus other methods that you might need
    • Have each DLL implement that interface
    • Each time you instantiate a child form (myclass1.dll), add the variable referencing the child form into a private array.
    • When the cild form closes the ReleaseMe event will be raised and caught in a common sink in the mdi container
    • The MDI container can then explicitly remove the variable that was referencing the child form from the private array.


    I hope it works, otherwise I'll be stuck also
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Chesterfield, UK
    Posts
    162

    Re: [RESOLVED] Howto Dispose of Child Form?

    I though I would test using the 'private' declaration before I go too much further.

    private x as myclass.program
    .
    x.mdiparent=me
    x.show
    .
    .
    x.dispose
    x=nothing
    gc.collect()

    As far as VB goes 'myclass' is no longer running but its still in memory.

    Any thoughts?

  13. #13
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: [RESOLVED] Howto Dispose of Child Form?

    Have you tried

    VB Code:
    1. private x as myclass.program
    2. .
    3. x.mdiparent=me
    4. x.show
    5. .
    6. .
    7. Try
    8. [INDENT]x.close()[/INDENT]
    9. [INDENT]x.dispose[/INDENT]
    10. Catch ex as Exception
    11. Finally
    12. [INDENT]x=nothing[/INDENT]
    13. [INDENT]gc.collect()[/INDENT]
    14. End Try
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Chesterfield, UK
    Posts
    162

    Re: [RESOLVED] Howto Dispose of Child Form?

    Just tried it but still no good. To be honest its probably worse, because the class is declared in a private variable it makes it in use straight away before the menu options been run.

    I've decided to give up on it, I'm happy with the menu options launching as they are and I cant see any benifit of delaring all the options in private variables.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width