When working with Windows forms in C#, is it safer/more beneficial resource-wise to completely close the form each time and reload the data when you need the form again, as opposed to just hiding the form and keeping it all in memory?
views:
174answers:
3I have never had a need to do this to improve on resource usage.
It is cleaner and feels safer to bring up a form when needed and close it once the purpose is served. Else, you have to write extra code to track whether/which form(s) are open and bring them to the front vs spawning a new one.
Why manage the resources yourself? You're just giving yourself more work if you were to keep references to forms and show/hide them instead of opening/closing them. If you ask me, it's better to allow the GC release all of the resources that any given form may be holding and close it.
Also, you'll find it hard to implement any sort of modal dialogs if you were to show/hide.
If a form is just a place to display data and allow for user input and the state information is held not in the form but in the underlying model, then there's probably no compelling reason not to just create a new form when you need it.
I don't think the form is the place to be holding any state info about your program and thus you ought to be able to close the form and make a new one without any issues.
That said, I don't always follow my own advice and state often creeps into my form code, probably because it is so convenient to do it.
I guess a consideration would be if there was significant construction necessary to bring a form and its various controls into existence, populate it with data and display it to the user.