【问题标题】:how to detect why form closing?如何检测为什么表格关闭?
【发布时间】:2020-05-23 04:28:32
【问题描述】:

如果表单/应用程序(在本文中讨论我使用 WinForms 在 C# 中创建了我自己的表单/应用程序)由于某种原因没有被用户关闭,例如 PC 突然关机或防病毒应用程序关闭我的应用程序,在这种情况下,当应用程序关闭时,我需要在同一个应用程序中创建您写入应用程序关闭原因的文件。 我有一个想法在 FormClosing 事件处理程序或 FormClosed 中创建一个文件,以便在 (FormClosingEventArgs)e.CloseReason 中写入原因,但它没有用......我决定通过 KillProcess 程序测试它(我用这个程序,因为例如,如果我通过 Windows 任务管理器停止应用程序,那么文件将被写入用户自己停止应用程序,所以我需要软件,以便我的应用程序不会确定我关闭它),关闭程序,但我的应用程序甚至没有时间闪烁,并不是说它会创建一个文件并在那里写入关于终止原因的数据,一般来说,它不会创建或写入任何东西。 执行此任务的选项有哪些?我需要一个应用来完成这一切,也就是说,不需要其他应用来分析我的应用。

带有处理程序的代码:

        private void UserForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        Console.WriteLine("Closing!!!");
        string path = @"C:\Users\Kulic\Desktop\reasonclose.txt";
        FileInfo fileInf = new FileInfo(path);

        switch (e.CloseReason)
        {
            case CloseReason.None:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The reason for the closure was not determined or cannot be determined.");                                
                        }
                    }
                    break;
                }

            case CloseReason.WindowsShutDown:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The operating system closes all applications before shutting down.");
                        }
                    }
                    break;
                }

            case CloseReason.MdiFormClosing:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The parent form of this multi-document interface (MDI) form is closed.");
                        }
                    }
                    break;
                }

            case CloseReason.UserClosing:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The form is closed programmatically or through user action in the UI (such as clicking the Close button in the window forms, select Close in the system menu of the window or by pressing ALT+F4).");
                        }
                    }
                    break;
                }

            case CloseReason.TaskManagerClosing:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("Microsoft Windows task Manager closes the application.");
                        }
                    }
                    break;
                }

            case CloseReason.FormOwnerClosing:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The owner form is closed.");
                        }
                    }
                    break;
                }

            case CloseReason.ApplicationExitCall:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The Exit() method of the Application class was called.");
                        }
                    }
                    break;
                }

            default:
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("The reason for closing was not revealed");
                        }
                    }
                    break;
                }
        }
    }

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    FormClosing 事件只有在程序或表单通过正常方式关闭时才有机会触发。使用任务管理器终止进程(或任何其他突然关闭它的方法)只需跳过它和任何其他关闭代码。

    这不是您的代码中的错误,而是进程终止的本质的结果:现在停止这件事,无论如何。在这种情况下,甚至没有任何机会运行,因为您的进程已被杀死。想象一下这样一种情况,你刚拔掉电脑,它就关机了,什么都不能正常关机。

    没有办法捕捉到这样的事件。你可以得到正常的关闭原因,但不是异常的。

    【讨论】:

    • 我需要做点别的事情:关闭我的应用程序后,我需要一个表单出现,用户可以在其中指定他们关闭应用程序原因的版本。根据你的回答,这是不可能的,我说的对吗?
    • @MaxWellViksna 正确,当进程被杀死或崩溃或以其他方式异常结束时。当程序正常关闭时,您的FormClosing 将按您的意愿运行,您可以启动另一个表单或注册或其他任何方式。
    【解决方案2】:

    您也可以尝试订阅 AppDomain.ProcessExit 事件 (documentation),但请记住,即使这样也不能保证在所有情况下都会触发。这完全取决于你的程序是如何被杀死的。

    例如,如果计算机意外断电或崩溃,那么nothing将在此之后运行,并且无法将关闭原因保存到文件中。

    此外,这超出了您的问题范围,但该代码过于复杂。您只需要检查File.Exists 并打开一次流。您可以将 switch 语句放在 using 块内。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 2013-01-06
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多