Reputation: 16330
This is my Program class:
static class Program
{
private class MyAppContext : ApplicationContext
{
public MyAppContext()
{
//Exit delegate
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
//Show the main form
MainForm main = new MainForm();
main.Show();
}
private void OnApplicationExit(object sender, EventArgs e)
{
//Cleanup code...
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyAppContext());
}
}
When I'm running the app in debug mode and I close it, the main form closes, but the app is still running and I have to click on Stop Debugging
to terminate it completely.
In the main form, all I do is:
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
Application.Exit();
}
What am I doing wrong?
Upvotes: 1
Views: 218
Reputation: 65692
The problem is inheriting the AppContext, not sure why you need that. Suggest you do it normally without AppContext and cleanup in the MainForm_Closing, OnFormClosed, App_Exit or similar event.
public class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new MyAppContext()); <- no need
Application.Run(new MainForm());
}
}
Upvotes: 2
Reputation: 1441
Try that:
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
Environment.Exit(0);
}
Upvotes: 0
Reputation: 8295
EDIT:
This works fine for me. So it must be something else wrong. Post more code.
public class Program
{
private class MyAppContext : ApplicationContext
{
public MyAppContext()
: base(new Form())
{
//Exit delegate
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
MainForm.Show();
}
private void OnApplicationExit(object sender, EventArgs e)
{
//Cleanup code...
}
}
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyAppContext());
}
}
Upvotes: 0