How do I prevent the app from terminating when I close the startup form?
The auto-generated code in Program.cs was written to terminate the application when the startup window is closed. You'll need to tweak it so it only terminates when there are no more windows left. Like this:
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var main = new Form1();
main.FormClosed += new FormClosedEventHandler(FormClosed);
main.Show();
Application.Run();
}
static void FormClosed(object sender, FormClosedEventArgs e) {
((Form)sender).FormClosed -= FormClosed;
if (Application.OpenForms.Count == 0) Application.ExitThread();
else Application.OpenForms[0].FormClosed += FormClosed;
}
startup form.close and application.exit not terminating app
You need an Exit While
after the Forms.Application.Exit
.
Application.Exit
still allows the threads to finish what they are doing, and since you are effectively in an infinite loop it will never actually let the application close.
I want to leave the application when the main form is closed
if you want to exit application then add this line
Application.Exit();
to detect if main form is closed, you need to use FormClosingEventHandler
and after detecting that main form is closed, then exit application
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.yourMainForm_FormClosing);
private void yourMainForm_FormClosing(object sender, FormClosingEventArgs e)
{
//here you can exit from application
Application.Exit();
}
Closing and re-opening a form without closing the application
Remove your Application.Exit()
. Since you are already in the FormClosing Event Handler, the Application will exit if Program.KeepRunning
is set to false.
Form remains open in the background after closing it
When you write:
Form1 x = new Form1();
you are creating a new Form1
object, so x
refers to the new one and not to the original one. Instead, you could use this:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var form2 = new Form2())
{
this.Hide();
form2.ShowDialog();
}
this.Show();
}
When ShowDialog()
is called, the code following it is not executed until after the dialog box is closed, so this.Show()
will execute only after Form2
is closed.
Related Topics
String Was Not Recognized as a Valid Datetime " Format Dd/Mm/Yyyy"
Ef Codefirst: Should I Initialize Navigation Properties
How to Call Stored Procedure in Entity Framework 6 (Code-First)
Json.Net: How to Deserialize Without Using the Default Constructor
Why Is Jsonrequestbehavior Needed
Download File of Any Type in ASP.NET MVC Using Fileresult
What Is the Yield Keyword Used For in C#
In .Net, Which Loop Runs Faster, 'For' or 'Foreach'
Unity Scripts Edited in Visual Studio Don't Provide Autocomplete
How to Add a Timeout to Console.Readline()
Volatile Vs. Interlocked Vs. Lock
Copy the Entire Contents of a Directory in C#
How to Remove All HTML Tags from a String Without Knowing Which Tags Are in It
What's the Use/Meaning of the @ Character in Variable Names in C#