0

在用户选择退出 WinForms 程序后,有没有比这更好的方法来处理做某事的任务:

[编辑 1:回应 'NoBugz 的评论]在这种情况下,表单上没有 ControlBox,并且有理由在用户选择关闭表单时发生的事情中设置一层间接性 [/edit 1]

[编辑 2:响应格林威治标准时间 +7 18:35 1 月 20 日的所有评论]也许使用淡出 MainForm 是一个简单的说明,说明您在关闭应用程序时可能想要做什么:用户无法与之交互:不言而喻,这与用户终止应用程序的决定有关。[/编辑 2]

(使用某种形式的线程?)(对多线程应用程序的影响?)(这段代码“难闻”吗?)

    // 'shutDown is an external form-scoped boolean variable
    //
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        // make sure we don't block Windows ShutDown
        // or other valid reasons to close the Form
        if (e.CloseReason != CloseReason.ApplicationExitCall) return;

        // test for 'shutDown flag set here
        if (shutDown) return;

        // cancel closing the Form this time through
        e.Cancel = true;

        // user choice : default = 'Cancel
        if (MessageBox.Show("Exit Application ?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
        {
            // user says Exit : activate Timer, set the flag to detect Exit
            timer1.Enabled = true;
            shutDown = true;
        }
    }

摘要:在一个非常标准的 WinForms 应用程序中(一个 MainForm 在 Program.cs 中以标准方式启动):在 MainForm 的 FormClosing 事件处理程序中:

  1. 立即退出(触发默认行为:关闭 MainForm 并退出应用程序)如果:

    一种。CloseReason 是任何其他 CloseReason.ApplicationExitCall

    湾。如果一个特殊的布尔变量设置为 true,或者

  2. 如果没有立即退出:取消对 FormClosing 的“第一次调用”。

  3. 然后用户通过 MessageBox.Show 对话框选择退出应用程序或取消:

    一种。如果用户取消,当然,应用程序保持“原样”。

    湾。如果用户选择“退出”:

    1. 将特殊的布尔标志变量设置为 true

    2. 运行一个 Timer 来做一些特殊的事情。

    3. 当 Timer 代码中的内部测试检测到“特殊内容”已完成时,它会调用 Application.Exit

4

3 回答 3

7

作为开发人员和用户,我的建议:

一个非常快的任务

  • 只需在 Closing 事件处理程序中执行任务。

一个不太快,但不是非常慢的任务

  • 使用 Closing 事件处理程序中的任务创建一个非后台线程(因此当应用程序退出时它不会关闭)。
  • 让应用程序退出。表单将消失,等等,但该过程将继续运行,直到任务完成。
  • 请记住在该工作线程中处理异常等。并确保如果用户在该任务完成之前重新打开您的应用程序,事情不会崩溃。

较慢的任务

  • 在 Closing 事件处理程序中,打开一个正在关闭的表单并让表单本身关闭。
  • 在显示一些友好的进度和信息的同时,在关闭表单中/后面执行任务。
  • 任务完成后退出应用程序。

一些未经测试的示例代码。我们正在我们的一个应用程序中做类似的事情。在我们的案例中,任务是将窗口属性(位置、大小、窗口状态等)存储到数据库中。

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    // If it wasn't the user who asked for the closing, we just close
    if (e.CloseReason != CloseReason.UserClosing)
        return;

    // If it was the user, we want to make sure he didn't do it by accident
    DialogResult r = MessageBox.Show("Are you sure you want this?", 
                                     "Application is shutting down.",
                                     MessageBoxButtons.YesNo, 
                                     MessageBoxIcon.Question);
    if (r != DialogResult.Yes)
    {
        e.Cancel = true;
        return;
    }
}

protected override void OnFormClosed(FormClosedEventArgs e)
{
    // Start the task
    var thread = new Thread(DoTask)
    {
        IsBackground = false,
        Name = "Closing thread.",
    };
    thread.Start();

    base.OnFormClosed(e);
}

private void DoTask()
{
    // Some task to do here
}
于 2010-01-20T11:49:34.663 回答
2

我看不出有什么“错误”。我唯一的建议是让用户知道程序正在“关闭”,方法是在系统托盘上方打开一个非模式窗口或通知烤面包机。

于 2010-01-19T19:33:12.380 回答
0

几个小点:

  • 我会质疑计时器的必要性:由于应用程序无论如何都会退出,因此用户不会期望 UI 响应,为什么不简单地在 UI 线程上执行清理代码呢?正如@Dave Swersky 所建议的,清理期间的一个小通知窗口会很有礼貌。

  • 如果应用程序退出由 Windows 关闭或用户注销触发,会发生什么情况?

于 2010-01-19T19:40:01.427 回答