removing VB.NET form from memory Best answer on the web
Posted in: darrelrussell.com edit
07 Jan 2009
allowing a user to select a profile/login from a small login window,
and once they login, it opens up the main form (maximized) and closes
the login form. The problem is when you close/terminate the main
form, the application doesn't unload from memory, because the login
form is still loaded. When the user logs in on the login form, I
create a new instance of the main form, and turn the visible property
to false on the login form. But when I close the main form, I still
have an invisble login form in memory. I tried to dispose or close the login form when the user logs in, but it also closes the main form (as it trys to open). What is the best way to address this problem. When the user closes the main form, I want the entire program to be removed from memory.
This is an interesting one, because VB.NET to some extent conceals what's "really" going on from you. If you were to compare a VB.NET Windows Forms application to a C# one, you'd see that in the C# application there is a method in the main form which looks like this:
public static void Main ()
{
Application.Run (new Form1 ()) ;
}
Application.Run () is the method that actually controls any Windows Forms application - and when a form is passed to it, it runs for only as long as that form is open. When it is closed, Application.Run () exits, and so, given the Main method above, so does the application.
VB.NET, although it doesn't show it in the code, actually does have this same method - it's quietly inserted for you into whatever form you have specified as the startup object in the Project Properties dialog box - so whenever that form is closed, your application automatically exits.
The best way to solve this in applications like yours, I have found, is to set the startup object in the Project Properties dialog box to "Sub Main". This restores the C#-style behaviour of requiring an explicit "Main" method somewhere in the application. Then, add a method to the main form which looks like this:
Public Shared Sub Main()
Dim f1 As Form1
f1 = New Form1
f1.Show()
Application.Run()
End Sub
where f1 is the form which you wish to be shown when the application starts, in your case, the login form. You can then open other forms from this first form and close it, and the application will continue to run.
The thing to note here is that the Application.Run() method now has *no* form specified. When this is the case, the application will run forever regardless of whether any forms at all or open - in short, we've just created an application with no way to exit, yet.
When you use this technique, you need to provide an explicit statement of when you wish to exit the application, by calling the Application.Exit() method. If you wish this to happen when the main form is closed, for example, you need to add a handler for the "Closed" event to the main form:
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed Application.Exit()
End Sub
which will call Application.Exit () at the appropriate time. Using the Closed event for this saves duplicating code in various handlers for menus, the close box, and so forth.
Another possible technique to achieve this is to set the startup object in the Project Properties dialog to your main form, configure that form to start as invisible, and then show your login form from it - making it visible again and closing the login form when the login is complete. This solves the problem in this specific case, as the main form is now the one which will cause the application to terminate on its being closed, but this has the disadvantage of only being applicable to the two-form case and would require considerable rewriting should additional "main" forms be added in the future. As the solution I outline above is more generalisable and upgradable, I consider it superior.
If this answer isn't quite what you're looking for, please feel free
to request a clarification,
cerebrate-ga
Search strategy:
Personal knowledge & Visual Studio .NET library.