예제 #1
0
        private async void ActionButton_Click(object sender, EventArgs args)
        {
            bool shiftKeyPressed = ModifierKeys == Keys.Shift;

            actionButton.Enabled = false;
            try
            {
                if (!justRestart)
                {
                    if (isHyperVActive == true)
                    {
                        if (!await SetHyperVStatus(false))
                        {
                            MessageBox.Show("Deactivating Hyper-V failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else if (isHyperVActive == false)
                    {
                        if (!await SetHyperVStatus(true))
                        {
                            MessageBox.Show("Activating Hyper-V failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else
                    {
                        return;                           // Should not happen
                    }
                }

                if (!shiftKeyPressed)
                {
                    if (!SafeNativeMethods.ExitWindowsEx(
                            ExitWindows.Reboot,
                            ShutdownReason.MajorOperatingSystem | ShutdownReason.MinorReconfig | ShutdownReason.FlagPlanned))
                    {
                        //int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                        //string errorMessage = new System.ComponentModel.Win32Exception(error).Message;
                        //MessageBox.Show($"Restarting the computer failed. {errorMessage} (Error {error}) Trying another method…", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        // ExitWindowsEx fails on Windows 10.
                        // Use the system command, there's no feedback from it.
                        Process.Start("shutdown.exe", "-r -t 0");
                    }
                }
            }
            finally
            {
                actionButton.Enabled = true;
            }

            // System is restarted. Prevent further actions
            Application.Exit();
        }
예제 #2
0
        public static void RebootMachine()
        {
            if (!SafeNativeMethods.ExitWindowsEx(
                    ExitWindows.Reboot,
                    ShutdownReason.MajorOperatingSystem | ShutdownReason.MinorReconfig | ShutdownReason.FlagPlanned))
            {
                //int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                //string errorMessage = new System.ComponentModel.Win32Exception(error).Message;
                //MessageBox.Show($"Restarting the computer failed. {errorMessage} (Error {error}) Trying another method…", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                // ExitWindowsEx fails on Windows 10.
                // Use the system command, there's no feedback from it.
                Process.Start("shutdown.exe", "-r -t 0");
            }
        }