コード例 #1
0
        private async void Action_Click(object sender, EventArgs e)
        {
            switch (isHyperVActive)
            {
            case true:
                if (!await HyperV.SetStatus(false))
                {
                    MessageBox.Show("Deactivating Hyper-V failed.", "Error", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    return;
                }
                break;

            case false:
                if (!await HyperV.SetStatus(true))
                {
                    MessageBox.Show("Activating Hyper-V failed.", "Error", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    return;
                }
                break;
            }

            HyperV.RebootMachine();

            Application.Exit();
        }
コード例 #2
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 HyperV.SetStatus(false))
                        {
                            MessageBox.Show("Deactivating Hyper-V failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else if (isHyperVActive == false)
                    {
                        if (!await HyperV.SetStatus(true))
                        {
                            MessageBox.Show("Activating Hyper-V failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else
                    {
                        return;   // Should not happen
                    }
                }

                if (!shiftKeyPressed)
                {
                    HyperV.RebootMachine();
                }
            }
            finally
            {
                actionButton.Enabled = true;
            }

            // System is restarted. Prevent further actions
            Application.Exit();
        }
コード例 #3
0
        private async void MainForm_Load(object sender, EventArgs args)
        {
            isHyperVActive = await HyperV.GetStatus();

            isHyperVRunning = HyperV.GetRunning();

            actionButton.Visible = true;
            actionButton.Focus();

            if (isHyperVActive == true)
            {
                statusLabel.Text      = "Hyper-V is ACTIVE.";
                statusLabel.ForeColor = Color.ForestGreen;
                actionButton.Text     = "Deactivate Hyper-V and restart computer";
                if (isHyperVRunning == false)
                {
                    statusLabel.Text += " However, it is currently NOT running, so a restart may be pending.";
                    justRestart       = true;
                    actionButton.Text = "Restart computer";
                }
            }
            else if (isHyperVActive == false)
            {
                statusLabel.Text      = "Hyper-V is DEACTIVATED.";
                statusLabel.ForeColor = Color.Firebrick;
                actionButton.Text     = "Activate Hyper-V and restart computer";
                if (isHyperVRunning == true)
                {
                    statusLabel.Text += " However, it is currently running, so a restart may be pending.";
                    justRestart       = true;
                    actionButton.Text = "Restart computer";
                }
            }
            else
            {
                statusLabel.Text      = "The current state of Hyper-V is UNKNOWN. The Hyper-V role may not be installed on this computer.";
                statusLabel.ForeColor = SystemColors.WindowText;
                actionButton.Text     = "No action available";
                actionButton.Enabled  = false;
            }
        }
コード例 #4
0
        public async Task <ContextMenuStrip> CreateAsync()
        {
            // Add the default menu options.
            var menu = new ContextMenuStrip();


            // Actions.
            isHyperVActive = await HyperV.GetStatus();

            var status = new ToolStripMenuItem();
            var action = new ToolStripMenuItem();

            status.Click += App_Click;
            action.Click += Action_Click;

            status.Font = new Font(status.Font, status.Font.Style | FontStyle.Bold);

            if (isHyperVActive == true)
            {
                status.Text      = "Hyper-V is ACTIVE.";
                status.ForeColor = Color.ForestGreen;
                action.Text      = "Deactivate Hyper-V and restart computer";
            }
            else if (isHyperVActive == false)
            {
                status.Text      = "Hyper-V is DEACTIVATED.";
                status.ForeColor = Color.Firebrick;
                action.Text      = "Activate Hyper-V and restart computer";
            }
            else
            {
                status.Text      = "The current state of Hyper-V is UNKNOWN. The Hyper-V role may not be installed on this computer.";
                status.ForeColor = SystemColors.WindowText;
                action.Text      = "No action available";
                action.Enabled   = false;
            }

            // Add Status.
            menu.Items.Add(status);

            // Separator.
            var sep = new ToolStripSeparator();

            menu.Items.Add(sep);

            // Add Action.
            menu.Items.Add(action);

            // Separator.
            sep = new ToolStripSeparator();
            menu.Items.Add(sep);

            // Exit.
            var item = new ToolStripMenuItem {
                Text = "Exit"
            };

            item.Click += Exit_Click;
            menu.Items.Add(item);

            return(menu);
        }