0

I have a main window and a few child windows. Child windows contain some data so I don't want them to be closed. So I hide them while in FormClosing I set e.Cancel = true;

But when I want the main window to close (exit application). I have some trouble to exit it. First it wouldn't close. But with an extra variable to note that I'm closing I need 2 clicks to actually close it (problem is before onFormClosing(mainWIndow, ...) is executed the framework tries to close the children which will hide themselves first).

// main and child windows have this as form closing event proc
private void onFormClosing(object sender, FormClosingEventArgs e)
{
    if (sender == this) // main
    {
        exitApp = true;
    }
    else
        if (sender == formChild) // child
        {
            if (!exitApp)
            {
                e.Cancel = true;
                formChild.Hide();
            }
        }
}

How can I close them all in 1 click ?

I would also like to avoid Application.Exit(); because I need to make sure all threads close properly. So best would be a clean close.

4

2 回答 2

1

您可以使用 Application.Exit() 强制应用程序。

于 2013-05-29T14:05:17.920 回答
0

自己找到了解决方案:

// ...
if (sender == this) // main
{
    exitApp = true;
    FormClosing -= onFormClosing;
    Close();
}
// ...
于 2013-05-30T08:46:57.050 回答