C# .NET - Application.Run() ? - Asked By Vlince on 20-Jul-14 02:25 PM

Introduction:
I’ve created a solution (MySolution.sln) containing a project (MyProject).
Inside MyProject, I’ve created an MDI form (mdiContainer), I’ve also created a single form (frmLogin), which enable users to enter their Username and Password with two buttons (Ok and Cancel). When the user clicks the Ok button, I want to close the frmLogin window and load the mdiContainer form.

NOTE:
I haven’t yet coded the validation process to see if that user exist in the database so my no need to check for that now.

Inside MyProject, I’ve also created a class (clsBeginApplication), which basically is the starting point of my C# application. It is inside this class (clsBeginApplication) that I create the main() function.


The code:
The main() function in the clsBeginApplication class looks something like this:

[STAThread]
static void Main() 
{
Application.Run(new frmLogin());
}


Now, I’ve set the clsBeginApplication class as being the “Startup Object” because it contains my main() function. When the code is executed the Application.Run fires and loads my frmLogin form. I then decide to click the Ok button and this is code inside the Ok button:

private void button1_Click(object sender, System.EventArgs e)
{
mdiContainer myMdi = new mdiContainer();
this.Close();
myMdi.Show();
}

This doesn’t seem to work, because of the “this.Close” instead of closing the frmLogin form and openning/showing the MDI form it stops my application (No error message, it just stops like if I’ve pressed the stop button in VS). What was suppose to happen was that the frmLogin would close then I would show the MDI form thus the “myMdi.Show();” piece of code.


So I said ok then, I’ll try .Hide instead of .Close so I change the code this.Close; to this.Hide and everything works great.


Questions:
1-My first question is why would .Close stop the execution of my application ?

2-If I use .Hide instead of .Close I’m assuming I’m only hiding the frmLogin but I don’t want it to be hidden I want it to be gone for ever. Is .Hide the equivalent to Me.Close like in Visual Basic ? I guess not. Is .Hide going to be cleaned up by the garbage collection later on since I’m not planning on using the frmLogin once the user presses the Ok button.

3- Also, if I use Application.Run in my class, shouldn’t I use Application.Exit to make my application stop ? I’m asking because when I used the “this.Close” it also made my application stop so I’m wondering…

4-Why would one use “Application.Run” when what I could’ve done in my clsBeginApplication class is:

[STAThread]
static void Main() 
{
//Application.Run(new frmLogin());

frmLogin objFrmLogin = new frmLogin();
objFrmLogin.ShowDialog();

}

What is the difference between using one method over the other ?
Asked By XIDONG ZHANG on 25-Jan-02 10:08 AM
When a form is closed, all resources created within the object are closed and the form is disposed. If the form you are closing is the startup form of your application, your application ends.
    Form.Hide(), inherited from Control, Hides the control. And hiding the control is acheived by by setting the Visible property to false.
    When a form is displayed as a modal dialog box, clicking the close form button (the button with an "X" at the top right of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Unlike modeless forms, the Close method is not called by the framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application.
    Application.Exit() stops all running message loops on all threads and closes all windows of the application. This method does not actually force the application to exit, but rather causes all calls to Run to return. To exit a message loop for the current thread only, call ExitThread.
Typically, the main function of an application calls this method and passes it to the main window of the application.
    Application.Run() adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application. The Dispose method of the Form class will be called prior to the return of this method.
Sahil Kumar replied to Vlince on 20-Jul-14 02:26 PM

To distinguish between these two options, you can add a boolean property ExitButtonClicked to your form and set it to true in the button Click-Event right before you call Application.Exit().

Now you can ask this property within the FormClosing event and distinguish between those two options within the case UserClosing.

Example:

    public bool UserClosing { get; set; } 
 
   
public FormMain() 
   
{ 
       
InitializeComponent(); 
 
       
UserClosing = false; 
       
this.buttonExit.Click += new EventHandler(buttonExit_Click); 
       
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); 
   
} 
 
   
void buttonExit_Click(object sender, EventArgs e) 
   
{ 
       
UserClosing = true; 
       
this.Close(); 
   
} 
 
   
void Form1_FormClosing(object sender, FormClosingEventArgs e) 
   
{ 
       
switch (e.CloseReason) 
       
{ 
           
case CloseReason.ApplicationExitCall: 
               
break; 
           
case CloseReason.FormOwnerClosing: 
               
break; 
           
case CloseReason.MdiFormClosing: 
               
break; 
           
case CloseReason.None: 
               
break; 
           
case CloseReason.TaskManagerClosing: 
               
break; 
           
case CloseReason.UserClosing: 
               
if (UserClosing) 
               
{ 
                   
//what should happen if the user hitted the button? 
               
} 
               
else 
               
{ 
                   
//what should happen if the user hitted the x in the upper right corner? 
               
} 
               
break; 
           
case CloseReason.WindowsShutDown: 
               
break; 
           
default: 
               
break; 
       
} 
   
}