[英]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.