[英]Forms not closing properly if terminated due to program exit / main form close
在名为“Dev”的表单上,我有以下OnFormClosing函数,以确保在用户关闭表单时正确关闭线程。
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
dev.stopMultipleColorsWatcher();
}
如果我直接关闭“开发”表格,工作正常。 但是如果“Dev”形式因为主窗体(“Form1”)被关闭(导致程序退出)而OnFormClosing()
,则“Dev”中的OnFormClosing()
函数永远不会被调用,因此线程继续运行并且程序'需要通过任务管理器杀死进程。
我该如何解决? 我意识到我可以在“Form1”中添加一个OnFormClosing()
函数,然后在“Dev”中调用OnFormClosing()
函数,但我希望能有一些更干净的东西。
更新:
从主窗体打开开发表单:
private void btn_opendev_Click(object sender, EventArgs e)
{
Dev frm = new Dev();
frm.Show();
}
主窗体真的叫做“programname.UI.Main”(我知道对..),它在“Program.cs”中打开(所以程序的入口点):
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new programname.UI.Main());
}
代替
Dev frm = new Dev();
frm.Show();
尝试这个
Dev frm = new Dev();
frm.Show(this);
此方法显示子表单的所有者,并且转到主表单的所有事件都应转到子表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.