Windows 进程无法关闭我的应用程序 [英] Windows process can't close my application

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

问题描述

我有一个 windows 窗体应用程序,它正在数据库中写入一堆东西.前段时间我在询问如何确保所有请求(写入数据库)在窗体关闭之前都已完成的解决方案.

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);
      }
 }

我刚刚意识到当我单击 X 按钮并且表单开始关闭时这是有效的,但是当 pc 进入关闭阶段时它不是.任何想法?也许我需要更改其他事件而不是 FormClosing 上的事件?还有一件事.我已将申请进程的优先级保存为实时进程.

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.

推荐答案

如果您的进程花费的时间超过几秒钟,操作系统将在关机期间终止它.您唯一的希望是中止关闭,完成工作并再次调用.

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.

你的过程只需要几秒钟,并且依赖于 Form 的实例,所以这个解决方案不会创建一个新线程来完成工作.所以你应该 hide 防止使用交互的表单,因为在此进程运行时主线程将被锁定.

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.

如果关闭事件试图关闭应用程序.应用程序将中止关机.不过,它需要 administrator 权限才能执行此操作.

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();
        }
    }
}

这篇关于Windows 进程无法关闭我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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