Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buttons to refresh the current HyperV activation state and to tog… #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions HyperVSwitch/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 69 additions & 2 deletions HyperVSwitch/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public MainForm()
statusLabel.Text = "Detecting current state…\nThis may take a few seconds.";
statusLabel.ForeColor = Color.Gray;
actionButton.Visible = false;

toggleButton.Visible = false;
refreshButton.Visible = false;
refreshButton.Text = "Refresh current state";

infoLabel.Text = "© 2016 Yves Goergen, GNU GPL v3";
}

Expand All @@ -39,6 +44,9 @@ private async void MainForm_Load(object sender, EventArgs args)
isHyperVActive = await GetHyperVStatus();
isHyperVRunning = GetHyperVRunning();

refreshButton.Visible = true;
toggleButton.Visible = true;

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

Expand All @@ -47,6 +55,7 @@ private async void MainForm_Load(object sender, EventArgs args)
statusLabel.Text = "Hyper-V is ACTIVE.";
statusLabel.ForeColor = Color.ForestGreen;
actionButton.Text = "Deactivate Hyper-V and restart computer";
toggleButton.Text = "Deactivate only (no restart)";
if (isHyperVRunning == false)
{
statusLabel.Text += " However, it is currently NOT running, so a restart may be pending.";
Expand All @@ -59,6 +68,7 @@ private async void MainForm_Load(object sender, EventArgs args)
statusLabel.Text = "Hyper-V is DEACTIVATED.";
statusLabel.ForeColor = Color.Firebrick;
actionButton.Text = "Activate Hyper-V and restart computer";
toggleButton.Text = "Activate only (no restart)";
if (isHyperVRunning == true)
{
statusLabel.Text += " However, it is currently running, so a restart may be pending.";
Expand All @@ -72,6 +82,8 @@ private async void MainForm_Load(object sender, EventArgs args)
statusLabel.ForeColor = SystemColors.WindowText;
actionButton.Text = "No action available";
actionButton.Enabled = false;
toggleButton.Visible = false;
toggleButton.Enabled = false;
}
}

Expand All @@ -84,11 +96,13 @@ private void MainForm_KeyDown(object sender, KeyEventArgs args)
if (args.KeyCode == Keys.F1 && args.Modifiers == 0)
{
string message =
"Hyper-V Switch allows you to enable or disable permanent virtualisation with Hyper-V without uninstalling it so that you can use Hyper-V and other virtualisation solutions like VMware or VirtualBox easily. This setting is stored in the boot configuration so that the computer must be restarted to apply the new setting.\n\n" +
"Hyper-V Switch allows you to enable or disable permanent virtualisation with Hyper-V without uninstalling it so that you can use Hyper-V " +
"and other virtualisation solutions like VMware or VirtualBox easily. This setting is stored in the boot configuration (bcdedit > hypervisorlaunchtype) " +
"so that the computer must be restarted to apply the new setting.\n\n" +
"For more information please click on the link to open the website.\n\n" +
"Available keyboard shortcuts:\n\n" +
"Escape: Close program\n" +
"Shift+Click: Change state but skip restart (you need to restart manually)";
"Shift+Click(" + actionButton.Text + "): Change state but skip restart (you need to restart manually)";
MessageBox.Show(message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Expand All @@ -101,6 +115,8 @@ private async void ActionButton_Click(object sender, EventArgs args)
{
bool shiftKeyPressed = ModifierKeys == Keys.Shift;
actionButton.Enabled = false;
refreshButton.Enabled = false;
toggleButton.Enabled = false;
try
{
if (!justRestart)
Expand Down Expand Up @@ -146,6 +162,8 @@ private async void ActionButton_Click(object sender, EventArgs args)
finally
{
actionButton.Enabled = true;
refreshButton.Enabled = true;
toggleButton.Enabled = true;
}

// System is restarted. Prevent further actions
Expand All @@ -157,6 +175,54 @@ private void InfoLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
Process.Start("http://unclassified.software/");
}

private void refreshButton_Click(object sender, EventArgs e)
{
actionButton.Enabled = false;
refreshButton.Enabled = false;
toggleButton.Enabled = false;
MainForm_Load(sender, e);
actionButton.Enabled = true;
refreshButton.Enabled = true;
toggleButton.Enabled = true;
}

private async void toggleButton_Click(object sender, EventArgs e)
{
actionButton.Enabled = false;
refreshButton.Enabled = false;
toggleButton.Enabled = false;
try
{
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
}
}
finally
{
MainForm_Load(sender, e);
actionButton.Enabled = true;
refreshButton.Enabled = true;
toggleButton.Enabled = true;
}
}

#endregion Control event handlers

#region Hyper-V support methods
Expand Down Expand Up @@ -208,5 +274,6 @@ private async Task<bool> SetHyperVStatus(bool active)
}

#endregion Hyper-V support methods

}
}