延迟申请关闭最佳做法?

Bil*_*llW 0 c# winforms formclosing

在用户选择退出WinForms程序后,是否有更好的方法来处理执行某项操作的任务:

:[编辑1 响应由"NoBugz评论 ] 在这种情况下,有形式没有控制盒,并有用于把一个间接层中,当用户选择关闭窗体[/编辑1]会发生什么原因

[编辑2:响应GMT +7 1月20日18:35的所有评论 ] 也许使用淡出MainForm可以简单说明您在关闭应用程序时可能需要做什么:用户无法与之交互:它显然与用户决定终止申请有关.[/ edit 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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

  1. 如果出现以下情况,请立即退出(触发默认行为:即关闭MainForm并退出应用程序)

    一个.CloseReason是其他任何CloseReason.ApplicationExitCall

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

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

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

    一个.如果用户取消,当然,应用程序保持"原样".

    湾 如果用户选择'退出:

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

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

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

Svi*_*ish 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
}
Run Code Online (Sandbox Code Playgroud)