这是我的课程课程:
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());
}
}
当我在调试模式下运行应用程序并关闭它时,主窗体关闭,但应用程序仍在运行,我必须单击Stop Debugging
才能完全终止它。
在主要形式中,我所做的只是:
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
Application.Exit();
}
我做错了什么?
答案 0 :(得分:2)
问题是继承AppContext,不确定为什么需要它。建议你在MainForm_Closing,OnFormClosed,App_Exit或类似事件中没有AppContext和cleanup的情况下正常进行。
public class Program
{
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new MyAppContext()); <- no need
Application.Run(new MainForm());
}
}
答案 1 :(得分:0)
修改强>
这对我来说很好。所以一定是别的错了。发布更多代码。
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());
}
}