Windows process can't close my application

2019-08-20 01:21发布

问题:

I have a windows form application which is writing bunch of stuff in the database. Now some time ago I was asking for the solution of how to be sure that all requests (writing to the database) has been done before windows form is closed.

The reason why am I doing this is because it will be a self standing application which has an automatic shut down and therefore I need to be able to finish all my writing and reading from the database and be sure when I am closing form that all the job is done.

Before I tried this solution I had a problem of cutting a connection when writing in the database because of a form closing.

Now the problem is that when I have a shut down process my windows form application won't close, so after some time it just cuts of the electricity which is not what I need.

Here is the code, and please tell me what is wrong with it? I need to close the form when program is sure that no task is working in the background.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
      // this.Visible = false; // optional
      // this.ShowInTaskbar = false; // optional
      Task db = Task.Factory.StartNew(() => DBUpdate());
      Task.WaitAll(db);
      if (this.shutdownRequested)
            Process.Start("shutdown.exe", "-s");
 }

 protected override void WndProc(ref Message ex)
 {
      if (ex.Msg == WM_QUERYENDSESSION)
      {
           Message MyMsg = new Message() { Msg = WM_CANCELMODE };
           base.WndProc(ref MyMsg);
           this.shutdownRequested = true;
      } else {
           base.WndProc(ref ex);
      }
 }

EDIT:

I just realized that this is working when I click the X button and form starts to close, but when pc goes into the shutdown phase then its not. Any idea? Maybe I need to change a event on something else and not on the FormClosing ? One more thing. I have saved a priority of the application process as a real-time process.

回答1:

If your process takes more than a couple of seconds, the OS will kill it during a shutdown. Your only hope is to abort the shutdown, do the work and call it again.

Your process only takes a couple of seconds, and is dependent on the instance of the Form, so this solution does not create a new thread to do the work in. So you should hide the form to prevent use interaction, as the main thread will be locked up while this process is run.

If a shutdown event tries to close the application. The application will abort the shutdown. It will need administrator privileges to be able to do this though.

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private bool _isClosing;
        private bool _isRunningUpdate;
        private bool _isShutdown;
        private Timer timer;

        public Form1()
        {
            InitializeComponent();
            FormClosing += Form1_FormClosing;
            timer = new Timer();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.WindowsShutDown)
            {
                // Abort Shutdown
                Process.Start("shutdown.exe", "-a");
                _isShutdown = true;
            }

            if (!_isRunningUpdate && _isClosing) return;

            // Set isClosing to true
            _isClosing = true;

            if (!_isRunningUpdate)
            {
                _isRunningUpdate = true;
                timer.Tick += DbUpdate;
                timer.Interval = 500;
                timer.Enabled = true; 
                timer.Start();
            }

            // Cancel current close request
            e.Cancel = true;

            // Optional Hide() --- could display message
            // Hide();
        }

        private void DbUpdate(object sender, EventArgs e)
        {
            timer.Stop();
            Thread.Sleep(3000);

            _isRunningUpdate = false;
            if (_isShutdown)
                Process.Start("shutdown.exe", "-s -t 10");
            if (_isClosing)
                Close();
        }
    }
}