-
Mar 15th, 2006, 12:19 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Howto Dispose of Child Form?
I have a MDI application with each menu option taking the following code
VB Code:
private sub menu1_click ...
dim x as new myclass.program
x.mdiparent=me
x.show()
end sub
I also test for open child forms by looping through me.children when I try to shutdown the app.
VB Code:
Dim openforms As Boolean = False
For Each x In Me.MdiChildren
openforms = True
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
-
Mar 15th, 2006, 12:28 PM
#2
Addicted Member
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
-
Mar 15th, 2006, 12:43 PM
#3
Thread Starter
Addicted Member
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.
-
Mar 15th, 2006, 12:46 PM
#4
Addicted Member
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
-
Mar 15th, 2006, 12:50 PM
#5
Thread Starter
Addicted Member
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.
-
Mar 15th, 2006, 01:07 PM
#6
Addicted Member
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
-
Mar 15th, 2006, 01:15 PM
#7
Thread Starter
Addicted Member
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.
-
Mar 15th, 2006, 01:18 PM
#8
Addicted Member
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
-
Mar 15th, 2006, 01:29 PM
#9
Fanatic Member
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:
try
For Each x As System.Windows.Forms.Control In Me.MdiChildren
x.Dispose()
Next
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 ...
-
Mar 15th, 2006, 01:41 PM
#10
Thread Starter
Addicted Member
Re: [RESOLVED] Howto Dispose of Child Form?
Adding GC.COLLECT made no difference.
-
Mar 15th, 2006, 01:56 PM
#11
Fanatic Member
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 ...
-
Mar 15th, 2006, 02:18 PM
#12
Thread Starter
Addicted Member
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?
-
Mar 16th, 2006, 09:50 AM
#13
Fanatic Member
Re: [RESOLVED] Howto Dispose of Child Form?
Have you tried
VB Code:
private x as myclass.program
.
x.mdiparent=me
x.show
.
.
Try
[INDENT]x.close()[/INDENT]
[INDENT]x.dispose[/INDENT]
Catch ex as Exception
Finally
[INDENT]x=nothing[/INDENT]
[INDENT]gc.collect()[/INDENT]
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 ...
-
Mar 16th, 2006, 10:01 AM
#14
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|