简体   繁体   中英

Application cancelling shutdown

I'm having a problem with an application that seems to cancel the shutdown or restart of my computer. I think it's because I capture the form closing event and cancel it like this:

private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!AllowApplicationClose)
    {
        e.Cancel = true;
        this.WindowState = FormWindowState.Minimized;
    }
}

I do this so that the close button just minimizes to the task bar. I do want to be able to restart without exiting the application though. Is there a better way to do this? Or a way to know if windows is closing the application?

EDIT: Thanks for the answers. Sorry I could only pick one as correct, I just went with the top one. Thanks again!

Check e.CloseReason first; only cancel if it's user initiated: CloseReason.UserClosing .

See here for other values you could use: CloseReason (MSDN)

您可以使用CloseReason来检测表单是由用户关闭还是更好(在您的情况下)使用SystemEvents -class在系统尝试注销或关闭/重新启动时获取事件,这样您也可以关闭应用程序当它被最小化。

检查事件参数的CloseReason ,如果它是WindowsShutDownTaskManagerClosing ,则不执行任何操作。

Use a threaded timer procedure then the below procedure to shut windows processes respectively.

public void Computer_Shutdown()
        {
            if (this.checkBox.Checked)
            {
                System.Diagnostics.Process[] processes = 
                   System.Diagnostics.Process.GetProcesses();

                foreach (System.Diagnostics.Process 
                           processParent in processes)
                {
                    System.Diagnostics.Process[] processNames = 
                                 System.Diagnostics.Process.
                                 GetProcessesByName
                                 (processParent.ProcessName);

                    foreach (System.Diagnostics.Process 
                            processChild in processNames)
                    {
                        try
                        {
                            System.IntPtr hWnd = 
                                   processChild.MainWindowHandle;

                            if (IsIconic(hWnd))
                            {
                                ShowWindowAsync(hWnd, SW_RESTORE);
                            }

                            SetForegroundWindow(hWnd);

                            if (!(processChild.
                               MainWindowTitle.Equals(this.Text)))
                            {
                                processChild.CloseMainWindow();
                                processChild.Kill();
                                processChild.WaitForExit();
                            }
                        }
                        catch (System.Exception exception)
                        {

                        }
                    }
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM