延迟应用程序关闭最佳实践? [英] delay Application Close best practice?

查看:133
本文介绍了延迟应用程序关闭最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户选择退出WinForms程序之后,是否有更好的方法处理该事情:

Is there a better way to handle the task of doing something after the user has chosen to exit a WinForms program than this :

在这种情况下,窗体上没有ControlBox ,因此有必要将一个级别的用户选择关闭表单[/edit 1]

[edit 1 : in response to comment by 'NoBugz] In this case there is no ControlBox on the Form, and there is a reason for putting one level of indirection in what happens when the user chooses to close the Form [/edit 1]

也许使用淡化MainForm可以简单地说明您的情况可能由于关闭了应用程序而想做:用户无法与之交互:这显然与用户决定终止应用程序有关. [/edit 2]

[edit 2 : in response to all comments as of GMT +7 18:35 January 20 ] Perhaps using fading out the MainForm is a simple illustration of what you might want do as the Application is being closed : the user cannot interact with that : it is self-evidently related to the user's decision to terminate the application. [/edit 2]

(使用某种形式的线程吗?)(是否暗示多线程应用程序?)(此代码难闻"吗?)

(use some form of threading ?) (implications for a multi-threaded app ?) (does this code "smell bad" ?)

    // '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应用程序(一个以标准方式在Program.cs中启动的MainForm)中:在MainForm的FormClosing事件处理程序中:

Summary : In a very standard WinForms application (one MainForm launched in Program.cs in the standard way) : in the FormClosing Event handler of the MainForm :

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

  1. exit immediately (triggering the default behavior : which is to close the MainForm and exit the Application) if :

a. CloseReason是其他CloseReason.ApplicationExitCall

a. the CloseReason is anything other CloseReason.ApplicationExitCall

b.如果将特殊的布尔变量设置为true,或者

b. if a special boolean variable is set to true, or

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

if no immediate exit : cancel the "first call" to FormClosing.

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

the user then makes a choice, via MessageBox.Show dialog, to Exit the Application, or Cancel :

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

a. if the user Cancels, of course, the Application stays "as is."

b.如果用户选择了退出:

b. if the user has chosen to 'Exit :

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

  1. set the special boolean flag variable to true

运行一个执行某些特殊任务的计时器.

run a Timer that does some special stuff.

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

when the internal test in the Timer code detects the "special stuff" is done, it calls Application.Exit

推荐答案

我的建议,无论是作为开发人员还是用户:

My suggestions, both as a developer and a user:

一项非常快速的任务

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

任务速度较慢,但​​速度并非如此慢

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

较慢的任务

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

一些未经测试的示例代码.我们正在其中一个应用程序中执行与此类似的操作.我们的任务是将窗口属性(位置,大小,窗口状态等)存储到数据库中.

Some untested example code. We are doing something similar to this in one of our applications. The task in our case is to store window properties (location, size, window state, et cetera) to a database.

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
}

这篇关于延迟应用程序关闭最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆