简体   繁体   English

消失的FormClosing / FormClosed事件

[英]Disappearing FormClosing/FormClosed events

Following code snippet may look weird, but it is a very simple method to reproduce the problem of a complicated interaction with a different application. 后面的代码片段看起来很奇怪,但是它是重现与其他应用程序进行复杂交互问题的非常简单的方法。

The example application has two forms, both forms register FormClosing and FormClosed events, just doing a Debug.WriteLine there. 该示例应用程序有两种形式,两种形式都注册FormClosing和FormClosed事件,只需在其中执行Debug.WriteLine。

The Main function does: Main函数执行以下操作:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Form2 form2 = new Form2();
    form2.Show();
    Application.Run(new Form1());
}

When I close Form2, I receive the events as expected. 当我关闭Form2时,我收到预期的事件。 When I close Form1, the application exits and Form2 gets closed too. 当我关闭Form1时,应用程序退出,Form2也被关闭。 But: the events of Form2 do not occur. 但是:Form2的事件不会发生。

I overrode the WndProc of Form2. 我覆盖了Form2的WndProc。 When I close Form2, I receive 当我关闭Form2时,我收到

15.10.2012 10:25:04 WndProc - WM_CLOSE
15.10.2012 10:25:04 OnClosing
15.10.2012 10:25:04 Form2_FormClosing
15.10.2012 10:25:04 OnClosed
15.10.2012 10:25:04 Form2_FormClosed
...
15.10.2012 10:25:04 WndProc - WM_DESTROY
...
15.10.2012 10:25:04 WndProc - WM_NCDESTROY

When I close Form1, Form2 does not receive a WM_CLOSE. 当我关闭Form1时,Form2不会收到WM_CLOSE。 But WM_DESTROY and WM_NCDESTROY are received. 但是收到WM_DESTROY和WM_NCDESTROY。

Edit: A different way of getting this strange behavior is as follows: The project has three forms, Form1 and Form2 have each a button for opening the next form. 编辑:获得此奇怪行为的一种不同方法如下:该项目具有三个窗体,Form1和Form2都有一个用于打开下一个窗体的按钮。 In the button click event of form2, do: 在form2的按钮单击事件中,执行以下操作:

Form3 frm3 = new Form3();
frm3.Parent = null;
frm3.Show(null);

Again, register event handlers as described above. 同样,如上所述注册事件处理程序。 When closing form2, the application stays running (form1 is still shown), but also form3 gets closed, and no WM_CLOSE event received. 关闭form2时,应用程序保持运行状态(仍然显示form1),但同时form3也被关闭,并且未收到WM_CLOSE事件。 Note that I already set both Parent and Owner of the form to null. 请注意,我已经将窗体的“父级”和“所有者”设置为null。

What is the reason for that strange behavior? 这种奇怪行为的原因是什么?

WM_CLOSE message is sent to have an option of canceling closing. 发送了WM_CLOSE消息以选择取消关闭。 See this flowchart: 请参阅此流程图:

在此处输入图片说明

So, there is no need in this message, when window is about to be destroyed anyway. 因此,无论何时窗口将要被销毁,此消息中都不需要。 This is behavior by design. 这是设计使然。 When you are closing main form of application, Application.ThreadExit method is called. 当您关闭应用程序的主要形式时,将调用Application.ThreadExit方法。 It described as: 它描述为:

Exits the message loop on the current thread and closes all windows on the thread. 退出当前线程上的消息循环并关闭该线程上的所有窗口。

If you want all opened forms to receive WM_CLOSE message, you need to use Application.Exit instead: 如果希望所有打开的表单都收到WM_CLOSE消息,则需要使用Application.Exit代替:

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. 通知所有消息泵它们必须终止,然后在处理完消息后关闭所有应用程序窗口。

Thus simply subscribe to Form1_FormClosing event and inform all opened windows about closing: 因此,只需订阅Form1_FormClosing事件并通知所有打开的窗口有关关闭的信息:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Application.Exit();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM